@@ -194,22 +194,35 @@ package foo
194194 goos : []string {"darwin" },
195195 watchErrorPath : "foo" ,
196196 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 {
203202 return err
204203 }
204+ from := filepath .Join (tmp , "from.go" )
205+
205206 to := filepath .Join (root , "to.go" )
206- from := filepath .Join (dir , "from.go" )
207207 // Create the symbolic link to a non-existing file. This would
208208 // cause the watch registration to fail.
209209 if err := os .Symlink (to , from ); err != nil {
210210 return err
211211 }
212212
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+
213226 // Should be able to capture an error from [fsnotify.Watcher.Add].
214227 err := <- errs
215228 if err == nil {
@@ -244,7 +257,7 @@ package foo
244257
245258 // Once the watch registration is done, file events under the
246259 // 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 )
248261 },
249262 expectedEvents : []protocol.FileEvent {
250263 {URI : "foo" , Type : protocol .Created },
0 commit comments