diff --git a/README.md b/README.md index 3548255de..53f4451b5 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,8 @@ The sync script: - Normalizes `//go:generate` directives - Copies `microsoft/TypeScript/tsc/testdata` → `testdata` - Runs `go mod tidy` pre/post generation +- Runs `scripts/post-sync.sh` to re-apply local harness patches (e.g. stable help-version padding) and refresh version-sensitive baselines +- Set `SYNC_POST=0` to skip post-sync Automated sync: the **Sync** workflow checks for new stable TypeScript ≥ 7.0.0 tags and opens a PR labeled `sync`. After merge, **Release Tag** pushes a matching git tag and **GoReleaser** publishes a GitHub Release (library-only; no binaries). diff --git a/README_zh-cn.md b/README_zh-cn.md index 761ce6584..e7113a14d 100644 --- a/README_zh-cn.md +++ b/README_zh-cn.md @@ -50,6 +50,8 @@ import "github.com/buke/typescript-go-internal/v7/pkg/ast" - 规范化 `//go:generate` 指令 - 复制 `microsoft/TypeScript/tsc/testdata` → `testdata` - 在生成前后运行 `go mod tidy` +- 运行 `scripts/post-sync.sh`,重新应用本地 harness 补丁(如稳定的 help 版本 padding)并刷新与版本相关的基线 +- 设置 `SYNC_POST=0` 可跳过 post-sync 自动化:**Sync** workflow 会检查新的 TypeScript ≥ 7.0.0 稳定 tag 并开带 `sync` label 的 PR。合入后 **Release Tag** 会推送同名 git tag,**GoReleaser** 会创建 GitHub Release(仅库,无二进制)。 diff --git a/pkg/execute/tsc/help.go b/pkg/execute/tsc/help.go index a05464ae3..e69e999c9 100644 --- a/pkg/execute/tsc/help.go +++ b/pkg/execute/tsc/help.go @@ -13,7 +13,7 @@ import ( ) func PrintVersion(sys System, locale locale.Locale) { - fmt.Fprintln(sys.Writer(), diagnostics.Version_0.Localize(locale, core.Version())) + fmt.Fprintln(sys.Writer(), diagnostics.Version_0.Localize(locale, versionForDisplay(sys))) } func PrintHelp(sys System, locale locale.Locale, commandLine *tsoptions.ParsedCommandLine) { @@ -24,6 +24,17 @@ func PrintHelp(sys System, locale locale.Locale, commandLine *tsoptions.ParsedCo } } +// versionForDisplay returns the version string used in CLI help/version output. +// Tests may set TS_TEST_VERSION so header padding is computed against the same +// string later written into baselines (see harnessutil.FakeTSVersion), instead of +// core.Version() which changes length across releases and shifts spacing. +func versionForDisplay(sys System) string { + if v := sys.GetEnvironmentVariable("TS_TEST_VERSION"); v != "" { + return v + } + return core.Version() +} + func getOptionsForHelp(commandLine *tsoptions.ParsedCommandLine) []*tsoptions.CommandLineOption { // Sort our options by their names, (e.g. "--noImplicitAny" comes before "--watch") opts := slices.Clone(tsoptions.OptionsDeclarations) @@ -74,7 +85,7 @@ func printEasyHelp(sys System, locale locale.Locale, simpleOptions []*tsoptions. output = append(output, " ", desc.Localize(locale), "\n", "\n") } - msg := diagnostics.X_tsc_Colon_The_TypeScript_Compiler.Localize(locale) + " - " + diagnostics.Version_0.Localize(locale, core.Version()) + msg := diagnostics.X_tsc_Colon_The_TypeScript_Compiler.Localize(locale) + " - " + diagnostics.Version_0.Localize(locale, versionForDisplay(sys)) output = append(output, getHeader(sys, msg)...) output = append(output, colors.bold(diagnostics.COMMON_COMMANDS.Localize(locale)), "\n", "\n") @@ -109,7 +120,7 @@ func printEasyHelp(sys System, locale locale.Locale, simpleOptions []*tsoptions. func printAllHelp(sys System, locale locale.Locale, options []*tsoptions.CommandLineOption) { var output []string - msg := diagnostics.X_tsc_Colon_The_TypeScript_Compiler.Localize(locale) + " - " + diagnostics.Version_0.Localize(locale, core.Version()) + msg := diagnostics.X_tsc_Colon_The_TypeScript_Compiler.Localize(locale) + " - " + diagnostics.Version_0.Localize(locale, versionForDisplay(sys)) output = append(output, getHeader(sys, msg)...) // ALL COMPILER OPTIONS section @@ -134,7 +145,7 @@ func printAllHelp(sys System, locale locale.Locale, options []*tsoptions.Command func PrintBuildHelp(sys System, locale locale.Locale, buildOptions []*tsoptions.CommandLineOption) { var output []string - output = append(output, getHeader(sys, diagnostics.X_tsc_Colon_The_TypeScript_Compiler.Localize(locale)+" - "+diagnostics.Version_0.Localize(locale, core.Version()))...) + output = append(output, getHeader(sys, diagnostics.X_tsc_Colon_The_TypeScript_Compiler.Localize(locale)+" - "+diagnostics.Version_0.Localize(locale, versionForDisplay(sys)))...) before := diagnostics.Using_build_b_will_make_tsc_behave_more_like_a_build_orchestrator_than_a_compiler_This_is_used_to_trigger_building_composite_projects_which_you_can_learn_more_about_at_0.Localize(locale, "https://aka.ms/tsc-composite-builds") options := core.Filter(buildOptions, func(option *tsoptions.CommandLineOption) bool { return option != &tsoptions.TscBuildOption diff --git a/pkg/execute/tsctests/sys.go b/pkg/execute/tsctests/sys.go index a1b4923df..1a493e553 100644 --- a/pkg/execute/tsctests/sys.go +++ b/pkg/execute/tsctests/sys.go @@ -132,6 +132,16 @@ func newTestSys(tscInput *tscInput, forIncrementalCorrectness bool) *TestSys { CurrentDirectory: cwd, }, currentWrite) sys.env = tscInput.env + if sys.env == nil { + sys.env = map[string]string{} + } else { + sys.env = maps.Clone(sys.env) + } + // Default TS_TEST_VERSION so help/version header padding matches FakeTSVersion + // (stable across core.Version() length changes). See scripts/post-sync.sh. + if _, ok := sys.env["TS_TEST_VERSION"]; !ok { + sys.env["TS_TEST_VERSION"] = harnessutil.FakeTSVersion + } sys.forIncrementalCorrectness = forIncrementalCorrectness sys.mockWatchBackend = NewMockWatchBackend() sys.mockWatchBackend.DirectoryExists = sys.fs.FS.DirectoryExists diff --git a/scripts/patches/0001-ts-test-version-stable-help-padding.patch b/scripts/patches/0001-ts-test-version-stable-help-padding.patch new file mode 100644 index 000000000..5e60e0250 --- /dev/null +++ b/scripts/patches/0001-ts-test-version-stable-help-padding.patch @@ -0,0 +1,79 @@ +diff --git a/pkg/execute/tsc/help.go b/pkg/execute/tsc/help.go +index a05464ae3..e69e999c9 100644 +--- a/pkg/execute/tsc/help.go ++++ b/pkg/execute/tsc/help.go +@@ -13,7 +13,7 @@ import ( + ) + + func PrintVersion(sys System, locale locale.Locale) { +- fmt.Fprintln(sys.Writer(), diagnostics.Version_0.Localize(locale, core.Version())) ++ fmt.Fprintln(sys.Writer(), diagnostics.Version_0.Localize(locale, versionForDisplay(sys))) + } + + func PrintHelp(sys System, locale locale.Locale, commandLine *tsoptions.ParsedCommandLine) { +@@ -24,6 +24,17 @@ func PrintHelp(sys System, locale locale.Locale, commandLine *tsoptions.ParsedCo + } + } + ++// versionForDisplay returns the version string used in CLI help/version output. ++// Tests may set TS_TEST_VERSION so header padding is computed against the same ++// string later written into baselines (see harnessutil.FakeTSVersion), instead of ++// core.Version() which changes length across releases and shifts spacing. ++func versionForDisplay(sys System) string { ++ if v := sys.GetEnvironmentVariable("TS_TEST_VERSION"); v != "" { ++ return v ++ } ++ return core.Version() ++} ++ + func getOptionsForHelp(commandLine *tsoptions.ParsedCommandLine) []*tsoptions.CommandLineOption { + // Sort our options by their names, (e.g. "--noImplicitAny" comes before "--watch") + opts := slices.Clone(tsoptions.OptionsDeclarations) +@@ -74,7 +85,7 @@ func printEasyHelp(sys System, locale locale.Locale, simpleOptions []*tsoptions. + output = append(output, " ", desc.Localize(locale), "\n", "\n") + } + +- msg := diagnostics.X_tsc_Colon_The_TypeScript_Compiler.Localize(locale) + " - " + diagnostics.Version_0.Localize(locale, core.Version()) ++ msg := diagnostics.X_tsc_Colon_The_TypeScript_Compiler.Localize(locale) + " - " + diagnostics.Version_0.Localize(locale, versionForDisplay(sys)) + output = append(output, getHeader(sys, msg)...) + + output = append(output, colors.bold(diagnostics.COMMON_COMMANDS.Localize(locale)), "\n", "\n") +@@ -109,7 +120,7 @@ func printEasyHelp(sys System, locale locale.Locale, simpleOptions []*tsoptions. + + func printAllHelp(sys System, locale locale.Locale, options []*tsoptions.CommandLineOption) { + var output []string +- msg := diagnostics.X_tsc_Colon_The_TypeScript_Compiler.Localize(locale) + " - " + diagnostics.Version_0.Localize(locale, core.Version()) ++ msg := diagnostics.X_tsc_Colon_The_TypeScript_Compiler.Localize(locale) + " - " + diagnostics.Version_0.Localize(locale, versionForDisplay(sys)) + output = append(output, getHeader(sys, msg)...) + + // ALL COMPILER OPTIONS section +@@ -134,7 +145,7 @@ func printAllHelp(sys System, locale locale.Locale, options []*tsoptions.Command + + func PrintBuildHelp(sys System, locale locale.Locale, buildOptions []*tsoptions.CommandLineOption) { + var output []string +- output = append(output, getHeader(sys, diagnostics.X_tsc_Colon_The_TypeScript_Compiler.Localize(locale)+" - "+diagnostics.Version_0.Localize(locale, core.Version()))...) ++ output = append(output, getHeader(sys, diagnostics.X_tsc_Colon_The_TypeScript_Compiler.Localize(locale)+" - "+diagnostics.Version_0.Localize(locale, versionForDisplay(sys)))...) + before := diagnostics.Using_build_b_will_make_tsc_behave_more_like_a_build_orchestrator_than_a_compiler_This_is_used_to_trigger_building_composite_projects_which_you_can_learn_more_about_at_0.Localize(locale, "https://aka.ms/tsc-composite-builds") + options := core.Filter(buildOptions, func(option *tsoptions.CommandLineOption) bool { + return option != &tsoptions.TscBuildOption +diff --git a/pkg/execute/tsctests/sys.go b/pkg/execute/tsctests/sys.go +index a1b4923df..1a493e553 100644 +--- a/pkg/execute/tsctests/sys.go ++++ b/pkg/execute/tsctests/sys.go +@@ -132,6 +132,16 @@ func newTestSys(tscInput *tscInput, forIncrementalCorrectness bool) *TestSys { + CurrentDirectory: cwd, + }, currentWrite) + sys.env = tscInput.env ++ if sys.env == nil { ++ sys.env = map[string]string{} ++ } else { ++ sys.env = maps.Clone(sys.env) ++ } ++ // Default TS_TEST_VERSION so help/version header padding matches FakeTSVersion ++ // (stable across core.Version() length changes). See scripts/post-sync.sh. ++ if _, ok := sys.env["TS_TEST_VERSION"]; !ok { ++ sys.env["TS_TEST_VERSION"] = harnessutil.FakeTSVersion ++ } + sys.forIncrementalCorrectness = forIncrementalCorrectness + sys.mockWatchBackend = NewMockWatchBackend() + sys.mockWatchBackend.DirectoryExists = sys.fs.FS.DirectoryExists diff --git a/scripts/patches/README.md b/scripts/patches/README.md new file mode 100644 index 000000000..fd73f6cb1 --- /dev/null +++ b/scripts/patches/README.md @@ -0,0 +1,8 @@ +# Sync patches + +Patches in this directory are re-applied by `scripts/post-sync.sh` after every +`scripts/sync-internal.sh` run (unless `SYNC_POST=0`). + +They encode local harness fixes that would otherwise be wiped when upstream +`tsc/internal` is copied into `pkg/`. Keep patches against a clean upstream- +synced tree (no local edits already applied). diff --git a/scripts/post-sync.sh b/scripts/post-sync.sh new file mode 100755 index 000000000..15b542177 --- /dev/null +++ b/scripts/post-sync.sh @@ -0,0 +1,70 @@ +#!/usr/bin/env bash +# Post-sync fixes applied after mirroring microsoft/TypeScript/tsc into this repo. +# Kept separate so upstream copies do not wipe local harness stability patches. +# +# Current fixes: +# 1) Re-apply scripts/patches/*.patch (TS_TEST_VERSION help padding) +# 2) Refresh version-sensitive tsc help baselines to match FakeTSVersion padding +set -euo pipefail + +ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +cd "$ROOT_DIR" + +PATCH_DIR="${ROOT_DIR}/scripts/patches" +POST_SYNC="${SYNC_POST:-1}" + +if [[ "${POST_SYNC}" != "1" ]]; then + echo "Skip post-sync (SYNC_POST=${POST_SYNC})" + exit 0 +fi + +echo "Running post-sync fixes..." + +apply_patches() { + if [[ ! -d "${PATCH_DIR}" ]]; then + return 0 + fi + local patch + shopt -s nullglob + for patch in "${PATCH_DIR}"/*.patch; do + echo "Applying $(basename "$patch")..." + if git apply --check "$patch" >/dev/null 2>&1; then + git apply "$patch" + elif grep -q 'TS_TEST_VERSION' pkg/execute/tsc/help.go 2>/dev/null; then + echo " already applied; skipping" + else + echo " ERROR: failed to apply $patch" >&2 + git apply --check "$patch" || true + exit 1 + fi + done +} + +refresh_help_baselines() { + echo "Refreshing version-sensitive help baselines..." + # Failure writes testdata/baselines/local/...; we then accept into reference/. + GOWORK=off go test ./pkg/execute/tsctests/ -count=1 \ + -run 'TestTscCommandline$/tsc/show_help_with_ExitStatus' >/dev/null 2>&1 || true + + local loc="testdata/baselines/local/tsc/commandLine" + local ref="testdata/baselines/reference/tsc/commandLine" + mkdir -p "$ref" + local f + for f in \ + show-help-with-ExitStatus.DiagnosticsPresent_OutputsSkipped.js \ + show-help-with-ExitStatus.DiagnosticsPresent_OutputsSkipped-when-host-cannot-provide-terminal-width.js + do + if [[ -f "${loc}/${f}" ]]; then + cp "${loc}/${f}" "${ref}/${f}" + echo " accepted ${f}" + fi + done + + GOWORK=off go test ./pkg/execute/tsctests/ -count=1 \ + -run 'TestTscCommandline$/tsc/show_help_with_ExitStatus' +} + +apply_patches +refresh_help_baselines + +echo "Post-sync complete." diff --git a/scripts/sync-internal.sh b/scripts/sync-internal.sh index 84cf09e3a..8f08ead7c 100755 --- a/scripts/sync-internal.sh +++ b/scripts/sync-internal.sh @@ -406,4 +406,10 @@ fi # Ensure testdata is present in repo (copy from submodule) copy_testdata +# Re-apply local harness patches and refresh version-sensitive baselines. +if [[ "${SYNC_POST:-1}" == "1" ]]; then + chmod +x scripts/post-sync.sh + ./scripts/post-sync.sh +fi + echo "Sync complete." \ No newline at end of file diff --git a/testdata/baselines/reference/tsc/commandLine/show-help-with-ExitStatus.DiagnosticsPresent_OutputsSkipped.js b/testdata/baselines/reference/tsc/commandLine/show-help-with-ExitStatus.DiagnosticsPresent_OutputsSkipped.js index 8e4558078..110eff88c 100644 --- a/testdata/baselines/reference/tsc/commandLine/show-help-with-ExitStatus.DiagnosticsPresent_OutputsSkipped.js +++ b/testdata/baselines/reference/tsc/commandLine/show-help-with-ExitStatus.DiagnosticsPresent_OutputsSkipped.js @@ -6,7 +6,7 @@ tsgo ExitStatus:: DiagnosticsPresent_OutputsSkipped Output:: Version FakeTSVersion -tsc: The TypeScript Compiler - Version FakeTSVersion   +tsc: The TypeScript Compiler - Version FakeTSVersion    TS  COMMON COMMANDS