Skip to content

Commit

Permalink
feat(gazelle): support generating ts_project(tsconfig) (#4627)
Browse files Browse the repository at this point in the history
If `ts_config` rules are being generated then automatically set
`ts_project(tsconfig)` to the closest tsconfig if one exists.

This maintains the existing restriction where only a single
`tsconfig.json` can be used per directory and that is used across all
generated `ts_project` targets. There is still a pending a feature to
customize this so different `ts_project` targets (main, specs,
custom...) can specify different tsconfig filenames/targets. See
`//workflows/scaling/gcp:gcp_tests` for an example where the "specs"
target manually specifies the `tsconfig` (overriding the default this PR
sets).

---

### Type of change

- New feature or functionality (change which adds functionality)

### Test plan

- Covered by existing test cases
- New test cases added

GitOrigin-RevId: 1670a5925b744903859e773b6ee1360c24cb1b92
  • Loading branch information
jbedard authored and alexeagle committed Feb 26, 2024
1 parent e41bd9e commit 4374292
Show file tree
Hide file tree
Showing 28 changed files with 44 additions and 7 deletions.
8 changes: 8 additions & 0 deletions gazelle/js/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,14 @@ func (ts *typeScriptLang) addProjectRule(cfg *JsGazelleConfig, args language.Gen
sourceRule.SetAttr("testonly", true)
}

if cfg.GetTsConfigGenerationEnabled() {
if rel, tsconfig := ts.tsconfig.ResolveConfig(args.Rel); tsconfig != nil {
tsconfigLabel := label.New("", rel, cfg.RenderTsConfigName(tsconfig.ConfigName))
tsconfigLabel = tsconfigLabel.Rel("", args.Rel)
sourceRule.SetAttr("tsconfig", tsconfigLabel.String())
}
}

adaptExistingRule(args, sourceRule)

result.Gen = append(result.Gen, sourceRule)
Expand Down
1 change: 1 addition & 0 deletions gazelle/js/tests/tsconfig_baseurl/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ load("@aspect_rules_ts//ts:defs.bzl", "ts_config", "ts_project")
ts_project(
name = "tsconfig_rootdir",
srcs = ["src/index.ts"],
tsconfig = ":tsconfig",
deps = [
"//src/lib/a",
"//src/lib/b",
Expand Down
1 change: 1 addition & 0 deletions gazelle/js/tests/tsconfig_baseurl/src/inheriter/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ load("@aspect_rules_ts//ts:defs.bzl", "ts_config", "ts_project")
ts_project(
name = "inheriter",
srcs = ["main.ts"],
tsconfig = ":tsconfig",
deps = [
"//src/inheriter/lib/d",
"//src/lib/c",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ load("@aspect_rules_ts//ts:defs.bzl", "ts_project")
ts_project(
name = "d",
srcs = ["index.ts"],
tsconfig = "//src/inheriter:tsconfig",
)
1 change: 1 addition & 0 deletions gazelle/js/tests/tsconfig_baseurl/src/lib/a/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ load("@aspect_rules_ts//ts:defs.bzl", "ts_project")
ts_project(
name = "a",
srcs = ["index.ts"],
tsconfig = "//:tsconfig",
)
1 change: 1 addition & 0 deletions gazelle/js/tests/tsconfig_baseurl/src/lib/b/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ load("@aspect_rules_ts//ts:defs.bzl", "ts_project")
ts_project(
name = "b",
srcs = ["index.ts"],
tsconfig = "//:tsconfig",
)
1 change: 1 addition & 0 deletions gazelle/js/tests/tsconfig_baseurl/src/lib/c/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ ts_project(
"first/c1.ts",
"second/c2.ts",
],
tsconfig = "//:tsconfig",
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ load("@aspect_rules_ts//ts:defs.bzl", "ts_config", "ts_project")
ts_project(
name = "p1",
srcs = ["index.ts"],
tsconfig = ":tsconfig",
deps = ["//src/projects/p1/nested_lib/a"],
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ load("@aspect_rules_ts//ts:defs.bzl", "ts_project")
ts_project(
name = "a",
srcs = ["a.ts"],
tsconfig = "//src/projects/p1:tsconfig",
)
1 change: 1 addition & 0 deletions gazelle/js/tests/tsconfig_deps/refs/a/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ load("@aspect_rules_ts//ts:defs.bzl", "ts_config", "ts_project")
ts_project(
name = "a",
srcs = ["index.ts"],
tsconfig = ":tsconfig",
)

ts_config(
Expand Down
1 change: 1 addition & 0 deletions gazelle/js/tests/tsconfig_deps/refs/b/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ load("@aspect_rules_ts//ts:defs.bzl", "ts_config", "ts_project")
ts_project(
name = "b",
srcs = ["index.ts"],
tsconfig = ":tsconfig",
deps = ["//refs/a"],
)

Expand Down
1 change: 1 addition & 0 deletions gazelle/js/tests/tsconfig_lax_json/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ load("@aspect_rules_ts//ts:defs.bzl", "ts_config", "ts_project")
ts_project(
name = "tsconfig_lax_json",
srcs = ["src/main.ts"],
tsconfig = ":tsconfig",
)

ts_config(
Expand Down
1 change: 1 addition & 0 deletions gazelle/js/tests/tsconfig_paths/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ load("@aspect_rules_ts//ts:defs.bzl", "ts_config", "ts_project")
ts_project(
name = "tsconfig_rootdir",
srcs = ["src/main.ts"],
tsconfig = ":tsconfig",
deps = [
"//fallback",
"//lib/a",
Expand Down
1 change: 1 addition & 0 deletions gazelle/js/tests/tsconfig_paths/custom/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ load("@aspect_rules_ts//ts:defs.bzl", "ts_config", "ts_project")
ts_project(
name = "custom",
srcs = ["main.ts"],
tsconfig = ":tsconfig",
deps = [
"//custom/nested",
"//fallback",
Expand Down
1 change: 1 addition & 0 deletions gazelle/js/tests/tsconfig_paths/custom/nested/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ load("@aspect_rules_ts//ts:defs.bzl", "ts_project")
ts_project(
name = "nested",
srcs = ["test.ts"],
tsconfig = "//custom:tsconfig",
deps = ["//lib/a"],
)
1 change: 1 addition & 0 deletions gazelle/js/tests/tsconfig_paths/fallback/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ ts_project(
"f1.ts",
"f2/a.ts",
],
tsconfig = "//:tsconfig",
deps = ["//lib/a"],
)
1 change: 1 addition & 0 deletions gazelle/js/tests/tsconfig_paths/inheriter/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ load("@aspect_rules_ts//ts:defs.bzl", "ts_config", "ts_project")
ts_project(
name = "inheriter",
srcs = ["main.ts"],
tsconfig = ":tsconfig",
deps = [
"//lib/a",
"//lib/c",
Expand Down
1 change: 1 addition & 0 deletions gazelle/js/tests/tsconfig_paths/lib/a/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ load("@aspect_rules_ts//ts:defs.bzl", "ts_project")
ts_project(
name = "a",
srcs = ["index.ts"],
tsconfig = "//:tsconfig",
)
1 change: 1 addition & 0 deletions gazelle/js/tests/tsconfig_paths/lib/b/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ load("@aspect_rules_ts//ts:defs.bzl", "ts_project")
ts_project(
name = "b",
srcs = ["index.ts"],
tsconfig = "//:tsconfig",
)
1 change: 1 addition & 0 deletions gazelle/js/tests/tsconfig_paths/lib/c/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ ts_project(
"first/c1.ts",
"second/c2.ts",
],
tsconfig = "//:tsconfig",
)
1 change: 1 addition & 0 deletions gazelle/js/tests/tsconfig_paths/overwriter/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Extend and overwrite some of a parent tsconfig
ts_project(
name = "overwriter",
srcs = ["main.ts"],
tsconfig = ":tsconfig",
deps = [
"//lib/a",
"//lib/c",
Expand Down
1 change: 1 addition & 0 deletions gazelle/js/tests/tsconfig_paths/overwriter2/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ ts_project(
"src/main.ts",
"src/overlib/o.ts",
],
tsconfig = ":tsconfig",
deps = [
"//lib/a",
"//lib/b",
Expand Down
1 change: 1 addition & 0 deletions gazelle/js/tests/tsconfig_rootdir/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ load("@aspect_rules_ts//ts:defs.bzl", "ts_config", "ts_project")
ts_project(
name = "tsconfig_rootdir",
srcs = ["src/main.ts"],
tsconfig = ":tsconfig",
)

ts_config(
Expand Down
1 change: 1 addition & 0 deletions gazelle/js/tests/tsconfig_rootdirs/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ load("@aspect_rules_ts//ts:defs.bzl", "ts_config", "ts_project")
ts_project(
name = "tsconfig_rootdirs",
srcs = ["src/main.ts"],
tsconfig = ":tsconfig",
deps = ["//roots/en"],
)

Expand Down
1 change: 1 addition & 0 deletions gazelle/js/tests/tsconfig_rootdirs/roots/en/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ load("@aspect_rules_ts//ts:defs.bzl", "ts_project")
ts_project(
name = "en",
srcs = ["lang/strings.ts"],
tsconfig = "//:tsconfig",
)
1 change: 1 addition & 0 deletions gazelle/js/tests/tsconfig_rootdirs/roots/fr/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ load("@aspect_rules_ts//ts:defs.bzl", "ts_project")
ts_project(
name = "fr",
srcs = ["lang/strings.ts"],
tsconfig = "//:tsconfig",
)
1 change: 1 addition & 0 deletions gazelle/js/tests/tsconfig_rootdirs/src2/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ load("@aspect_rules_ts//ts:defs.bzl", "ts_config", "ts_project")
ts_project(
name = "src2",
srcs = ["main.ts"],
tsconfig = ":tsconfig",
deps = ["//roots/fr"],
)

Expand Down
17 changes: 10 additions & 7 deletions gazelle/js/typescript/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,28 @@ func (tc *TsWorkspace) GetTsConfigFile(rel string) *TsConfig {
return c
}

func (tc *TsWorkspace) getConfig(f string) (string, *TsConfig) {
dir := f

for dir = f; dir != ""; {
dir = path.Dir(dir)
func (tc *TsWorkspace) ResolveConfig(dir string) (string, *TsConfig) {
for {
if dir == "." {
dir = ""
}

if tc.cm.configFiles[dir] != nil {
return dir, tc.GetTsConfigFile(dir)
}

if dir == "" {
break
}

dir = path.Dir(dir)
}

return "", nil
}

func (tc *TsWorkspace) IsWithinTsRoot(f string) bool {
dir, c := tc.getConfig(f)
dir, c := tc.ResolveConfig(path.Dir(f))
if c == nil {
return true
}
Expand All @@ -111,7 +114,7 @@ func (tc *TsWorkspace) IsWithinTsRoot(f string) bool {
}

func (tc *TsWorkspace) ExpandPaths(from, f string) []string {
_, c := tc.getConfig(from)
_, c := tc.ResolveConfig(path.Dir(from))
if c == nil {
return []string{}
}
Expand Down

0 comments on commit 4374292

Please sign in to comment.