Skip to content

Commit d0c9e32

Browse files
authored
fix --watch crash if no tsconfig found (#841)
1 parent 83fd62c commit d0c9e32

File tree

5 files changed

+64
-5
lines changed

5 files changed

+64
-5
lines changed

internal/execute/export_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ func CommandLineTestWatch(sys System, cb cbType, commandLineArgs []string) (*tso
1717
return parsedCommandLine, w
1818
}
1919

20+
func StartForTest(w *watcher) {
21+
// this function should perform any initializations before w.doCycle() in `start(watcher)`
22+
w.initialize()
23+
}
24+
2025
func RunWatchCycle(w *watcher) {
2126
// this function should perform the same stuff as w.doCycle() without printing time-related output
2227
if w.hasErrorsInTsConfig() {

internal/execute/verifytscwatch_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func verifyWatch(t *testing.T, test *tscInput, scenario string, edits []*testTsc
2727
baselineBuilder.WriteString("\n\n")
2828

2929
// build initial state
30+
execute.StartForTest(watcher)
3031
execute.RunWatchCycle(watcher)
3132
test.sys.serializeState(baselineBuilder)
3233

@@ -44,6 +45,29 @@ func verifyWatch(t *testing.T, test *tscInput, scenario string, edits []*testTsc
4445
})
4546
}
4647

48+
func TestWatch(t *testing.T) {
49+
t.Parallel()
50+
if !bundled.Embedded {
51+
// Without embedding, we'd need to read all of the lib files out from disk into the MapFS.
52+
// Just skip this for now.
53+
t.Skip("bundled files are not embedded")
54+
}
55+
56+
testCases := []*tscInput{
57+
{
58+
subScenario: "watch with no tsconfig",
59+
sys: newTestSys(FileMap{
60+
"/home/src/workspaces/project/index.ts": "",
61+
}, "/home/src/workspaces/project"),
62+
commandLineArgs: []string{"index.ts", "--watch"},
63+
},
64+
}
65+
66+
for _, test := range testCases {
67+
verifyWatch(t, test, "commandLineWatch", nil)
68+
}
69+
}
70+
4771
func listToTsconfig(base string, tsconfigOpts ...string) (string, string) {
4872
optionString := strings.Join(tsconfigOpts, ",\n ")
4973
tsconfigText := `{

internal/execute/watch.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ import (
88
)
99

1010
func start(w *watcher) ExitStatus {
11-
if w.configFileName == "" {
12-
w.host = compiler.NewCompilerHost(w.options.CompilerOptions(), w.sys.GetCurrentDirectory(), w.sys.FS(), w.sys.DefaultLibraryPath())
13-
}
11+
w.initialize()
12+
1413
watchInterval := 1000 * time.Millisecond
1514
if w.options.ParsedConfig.WatchOptions != nil {
1615
watchInterval = time.Duration(*w.options.ParsedConfig.WatchOptions.Interval) * time.Millisecond
@@ -21,7 +20,16 @@ func start(w *watcher) ExitStatus {
2120
}
2221
}
2322

23+
func (w *watcher) initialize() {
24+
// if this function is updated, make sure to update `StartForTest` in export_test.go as needed
25+
if w.configFileName == "" {
26+
w.host = compiler.NewCompilerHost(w.options.CompilerOptions(), w.sys.GetCurrentDirectory(), w.sys.FS(), w.sys.DefaultLibraryPath())
27+
}
28+
}
29+
2430
func (w *watcher) doCycle() {
31+
// if this function is updated, make sure to update `RunWatchCycle` in export_test.go as needed
32+
2533
if w.hasErrorsInTsConfig() {
2634
// these are unrecoverable errors--report them and do not build
2735
return

internal/execute/watcher.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@ type watcher struct {
2323
}
2424

2525
func createWatcher(sys System, configParseResult *tsoptions.ParsedCommandLine, reportDiagnostic diagnosticReporter) *watcher {
26-
return &watcher{
26+
w := &watcher{
2727
sys: sys,
28-
configFileName: configParseResult.ConfigFile.SourceFile.FileName(),
2928
options: configParseResult,
3029
reportDiagnostic: reportDiagnostic,
3130
// reportWatchStatus: createWatchStatusReporter(sys, configParseResult.CompilerOptions().Pretty),
3231
}
32+
if configParseResult.ConfigFile != nil {
33+
w.configFileName = configParseResult.ConfigFile.SourceFile.FileName()
34+
}
35+
return w
3336
}
3437

3538
func (w *watcher) compileAndEmit() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
currentDirectory::/home/src/workspaces/project
3+
useCaseSensitiveFileNames::true
4+
Input::index.ts --watch
5+
//// [/home/src/workspaces/project/index.ts] new file
6+
7+
8+
9+
10+
CompilerOptions::{
11+
"watch": true
12+
}
13+
14+
15+
Output::
16+
No output
17+
//// [/home/src/workspaces/project/index.ts] new file
18+
19+

0 commit comments

Comments
 (0)