@@ -194,22 +194,35 @@ package foo
194
194
goos : []string {"darwin" },
195
195
watchErrorPath : "foo" ,
196
196
changes : func (root string , errs chan error ) error {
197
- // ├── foo <- 1st
198
- // │ ├── from.go -> ../to.go <- 2nd
199
- // │ └── foo.go <- 4th
200
- // └── to.go <- 3rd
201
- dir := filepath .Join (root , "foo" )
202
- if err := os .Mkdir (dir , 0755 ); err != nil {
197
+ // Prepare a dir with with broken symbolic link.
198
+ // foo <- 1st
199
+ // └── from.go -> root/to.go <- 1st
200
+ tmp := filepath .Join (t .TempDir (), "foo" )
201
+ if err := os .Mkdir (tmp , 0755 ); err != nil {
203
202
return err
204
203
}
204
+ from := filepath .Join (tmp , "from.go" )
205
+
205
206
to := filepath .Join (root , "to.go" )
206
- from := filepath .Join (dir , "from.go" )
207
207
// Create the symbolic link to a non-existing file. This would
208
208
// cause the watch registration to fail.
209
209
if err := os .Symlink (to , from ); err != nil {
210
210
return err
211
211
}
212
212
213
+ // Move the directory containing the broken symlink into place
214
+ // to avoids a flaky test where the directory could be watched
215
+ // before the symlink is created. See golang/go#74782.
216
+ if err := os .Rename (tmp , filepath .Join (root , "foo" )); err != nil {
217
+ return err
218
+ }
219
+
220
+ // root
221
+ // ├── foo <- 2nd (Move)
222
+ // │ ├── from.go -> ../to.go <- 2nd (Move)
223
+ // │ └── foo.go <- 4th (Create)
224
+ // └── to.go <- 3rd (Create)
225
+
213
226
// Should be able to capture an error from [fsnotify.Watcher.Add].
214
227
err := <- errs
215
228
if err == nil {
@@ -244,7 +257,7 @@ package foo
244
257
245
258
// Once the watch registration is done, file events under the
246
259
// dir should be captured.
247
- return os .WriteFile (filepath .Join (dir , "foo.go" ), []byte ("package main" ), 0644 )
260
+ return os .WriteFile (filepath .Join (root , "foo" , "foo.go" ), []byte ("package main" ), 0644 )
248
261
},
249
262
expectedEvents : []protocol.FileEvent {
250
263
{URI : "foo" , Type : protocol .Created },
0 commit comments