Skip to content

Commit 19c84df

Browse files
authored
Exit with an exit code in tsgo (microsoft#689)
1 parent 0e7d032 commit 19c84df

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

cmd/tsgo/main.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,19 @@ func parseArgs() *cliOptions {
120120
}
121121

122122
func main() {
123+
os.Exit(runMain())
124+
}
125+
126+
func runMain() int {
123127
// TypeScript uses ANSI escape sequences which cmd.exe won't parse without enabling virtual terminal processing.
124128
enableVirtualTerminalProcessing()
125129

126130
if args := os.Args[1:]; len(args) > 0 {
127131
switch args[0] {
128132
case "tsc":
129-
os.Exit(int(execute.CommandLine(newSystem(), nil, args[1:])))
133+
return int(execute.CommandLine(newSystem(), nil, args[1:]))
130134
case "lsp":
131-
os.Exit(runLSP(args[1:]))
135+
return runLSP(args[1:])
132136
}
133137
}
134138
opts := parseArgs()
@@ -143,7 +147,7 @@ func main() {
143147
currentDirectory, err := os.Getwd()
144148
if err != nil {
145149
fmt.Fprintf(os.Stderr, "Error getting current directory: %v\n", err)
146-
os.Exit(1)
150+
return 1
147151
}
148152

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

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

179183
if compilerOptions.ListFilesOnly.IsTrue() {
180184
listFiles(program)
181-
os.Exit(0)
185+
return 0
182186
}
183187

184188
if compilerOptions.ShowConfig.IsTrue() {
185189
enc := json.NewEncoder(os.Stdout)
186190
enc.SetIndent("", " ")
187191
if err := enc.Encode(compilerOptions); err != nil {
188192
fmt.Fprintf(os.Stderr, "Error encoding JSON: %v\n", err)
189-
os.Exit(1)
193+
return 1
190194
}
191-
os.Exit(0)
195+
return 0
192196
}
193197

194198
var bindTime, checkTime time.Duration
195199

196200
diagnostics := program.GetConfigFileParsingDiagnostics()
197201
if len(diagnostics) != 0 {
198202
printDiagnostics(diagnostics, host, compilerOptions)
199-
os.Exit(1)
203+
return 1
200204
}
201205

202206
diagnostics = program.GetSyntacticDiagnostics(nil)
@@ -233,8 +237,12 @@ func main() {
233237
runtime.GC()
234238
runtime.ReadMemStats(&memStats)
235239

236-
if !opts.devel.quiet && len(diagnostics) != 0 {
237-
printDiagnostics(ts.SortAndDeduplicateDiagnostics(diagnostics), host, compilerOptions)
240+
exitCode := 0
241+
if len(diagnostics) != 0 {
242+
if !opts.devel.quiet {
243+
printDiagnostics(ts.SortAndDeduplicateDiagnostics(diagnostics), host, compilerOptions)
244+
}
245+
exitCode = 1
238246
}
239247

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

270278
stats.print()
279+
280+
return exitCode
271281
}
272282

273283
type tableRow struct {

0 commit comments

Comments
 (0)