Skip to content

Exit with an exit code in tsgo #689

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

Merged
merged 1 commit into from
Mar 24, 2025
Merged
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
30 changes: 20 additions & 10 deletions cmd/tsgo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,19 @@ func parseArgs() *cliOptions {
}

func main() {
os.Exit(runMain())
}

func runMain() int {
// TypeScript uses ANSI escape sequences which cmd.exe won't parse without enabling virtual terminal processing.
enableVirtualTerminalProcessing()

if args := os.Args[1:]; len(args) > 0 {
switch args[0] {
case "tsc":
os.Exit(int(execute.CommandLine(newSystem(), nil, args[1:])))
return int(execute.CommandLine(newSystem(), nil, args[1:]))
case "lsp":
os.Exit(runLSP(args[1:]))
return runLSP(args[1:])
}
}
opts := parseArgs()
Expand All @@ -143,7 +147,7 @@ func main() {
currentDirectory, err := os.Getwd()
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting current directory: %v\n", err)
os.Exit(1)
return 1
}

fs := bundled.WrapFS(osvfs.FS())
Expand All @@ -154,7 +158,7 @@ func main() {
configFileName = tspath.CombinePaths(configFileName, "tsconfig.json")
if !fs.FileExists(configFileName) {
fmt.Fprintf(os.Stderr, "Error: The file %v does not exist.\n", configFileName)
os.Exit(1)
return 1
}
}

Expand All @@ -178,25 +182,25 @@ func main() {

if compilerOptions.ListFilesOnly.IsTrue() {
listFiles(program)
os.Exit(0)
return 0
}

if compilerOptions.ShowConfig.IsTrue() {
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
if err := enc.Encode(compilerOptions); err != nil {
fmt.Fprintf(os.Stderr, "Error encoding JSON: %v\n", err)
os.Exit(1)
return 1
}
os.Exit(0)
return 0
}

var bindTime, checkTime time.Duration

diagnostics := program.GetConfigFileParsingDiagnostics()
if len(diagnostics) != 0 {
printDiagnostics(diagnostics, host, compilerOptions)
os.Exit(1)
return 1
}

diagnostics = program.GetSyntacticDiagnostics(nil)
Expand Down Expand Up @@ -233,8 +237,12 @@ func main() {
runtime.GC()
runtime.ReadMemStats(&memStats)

if !opts.devel.quiet && len(diagnostics) != 0 {
printDiagnostics(ts.SortAndDeduplicateDiagnostics(diagnostics), host, compilerOptions)
exitCode := 0
if len(diagnostics) != 0 {
if !opts.devel.quiet {
printDiagnostics(ts.SortAndDeduplicateDiagnostics(diagnostics), host, compilerOptions)
}
exitCode = 1
}

if exts := program.UnsupportedExtensions(); len(exts) != 0 {
Expand Down Expand Up @@ -268,6 +276,8 @@ func main() {
stats.add("Total time", totalTime)

stats.print()

return exitCode
}

type tableRow struct {
Expand Down