Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(typescript): don't declare outputs that collide with inputs #3046

Merged
merged 1 commit into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions packages/typescript/internal/ts_project.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 12 additions & 0 deletions packages/typescript/test/ts_project/allow_js/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
)
3 changes: 3 additions & 0 deletions packages/typescript/test/ts_project/allow_js/b.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';
exports.__esModule = true;
exports.b = <b></b>;
11 changes: 6 additions & 5 deletions packages/typescript/test/ts_project/allow_js/verify.js
Original file line number Diff line number Diff line change
@@ -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`);
}