diff --git a/packages/typescript/internal/ts_project.bzl b/packages/typescript/internal/ts_project.bzl index d08783ee09..5975db5f30 100644 --- a/packages/typescript/internal/ts_project.bzl +++ b/packages/typescript/internal/ts_project.bzl @@ -340,11 +340,16 @@ def _replace_ext(f, ext_map): def _out_paths(srcs, outdir, rootdir, allow_js, ext_map): rootdir_replace_pattern = rootdir + "/" if rootdir else "" - return [ - _join(outdir, f[:f.rindex(".")].replace(rootdir_replace_pattern, "") + _replace_ext(f, ext_map)) - for f in srcs - if _is_ts_src(f, allow_js) - ] + outs = [] + for f in srcs: + if _is_ts_src(f, allow_js): + out = _join(outdir, f[:f.rindex(".")].replace(rootdir_replace_pattern, "") + _replace_ext(f, ext_map)) + + # Don't declare outputs that collide with inputs + # for example, a.js -> a.js + if out != f: + outs.append(out) + return outs def ts_project_macro( name = "tsconfig", diff --git a/packages/typescript/test/ts_project/allow_js/BUILD.bazel b/packages/typescript/test/ts_project/allow_js/BUILD.bazel index fab23516b2..bcbcdbe76a 100644 --- a/packages/typescript/test/ts_project/allow_js/BUILD.bazel +++ b/packages/typescript/test/ts_project/allow_js/BUILD.bazel @@ -4,6 +4,7 @@ load("//packages/typescript:index.bzl", "ts_project") # Ensure that a.js produces outDir/a.js, outDir/a.d.ts, and outDir/a.d.ts.map SRCS = [ "a.js", + "b.jsx", ] ts_project( @@ -34,3 +35,14 @@ nodejs_test( "$(locations :transpile)", ], ) + +# Test that we can write outputs to the same folder as the inputs +# ts_project shouldn't try to declare a.js as an output in this case +ts_project( + name = "transpile_to_same_dir", + srcs = SRCS, + allow_js = True, + declaration = True, + declaration_map = True, + source_map = True, +) diff --git a/packages/typescript/test/ts_project/allow_js/b.jsx b/packages/typescript/test/ts_project/allow_js/b.jsx new file mode 100644 index 0000000000..be8667077b --- /dev/null +++ b/packages/typescript/test/ts_project/allow_js/b.jsx @@ -0,0 +1,3 @@ +'use strict'; +exports.__esModule = true; +exports.b = ; diff --git a/packages/typescript/test/ts_project/allow_js/verify.js b/packages/typescript/test/ts_project/allow_js/verify.js index 32b419ff19..8ea942aedf 100644 --- a/packages/typescript/test/ts_project/allow_js/verify.js +++ b/packages/typescript/test/ts_project/allow_js/verify.js @@ -1,7 +1,8 @@ const assert = require('assert'); -const types_files = process.argv.slice(2, 4); -const code_file = process.argv[4]; -assert.ok(types_files.some(f => f.endsWith('out/a.d.ts')), 'Missing a.d.ts'); -assert.ok(types_files.some(f => f.endsWith('out/a.d.ts.map')), 'Missing a.d.ts.map'); -assert.ok(code_file.endsWith('out/a.js'), 'Missing a.js'); +const output_files = process.argv.slice(2); +for (const file of ['a', 'b']) { + assert.ok(output_files.some(f => f.endsWith(`out/${file}.d.ts`)), `Missing ${file}.d.ts`); + assert.ok(output_files.some(f => f.endsWith(`out/${file}.d.ts.map`)), `Missing ${file}.d.ts.map`); + assert.ok(output_files.some(f => f.endsWith(`out/${file}.js`)), `Missing ${file}.js`); +}