From 310ea71b71739fd393f8828d091f07d19cae9690 Mon Sep 17 00:00:00 2001 From: Robert Findley Date: Mon, 1 Aug 2022 12:07:41 -0400 Subject: [PATCH] gopls/internal/regtest: add a test that ignoring a file resolves errors Add a test that when a user adds a "//go:build ignore" to a file, gopls correctly invalidates diagnostics. This used to be broken, but was fixed by CL 417576. Fixes golang/go#54147 Change-Id: I554fcfc0a56b72f657e19b3c0ae53a66d1a99a76 Reviewed-on: https://go-review.googlesource.com/c/tools/+/420537 TryBot-Result: Gopher Robot gopls-CI: kokoro Reviewed-by: Suzy Mueller Run-TryBot: Robert Findley --- .../regtest/workspace/metadata_test.go | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/gopls/internal/regtest/workspace/metadata_test.go b/gopls/internal/regtest/workspace/metadata_test.go index 28291a2e23..4c3f46b0a9 100644 --- a/gopls/internal/regtest/workspace/metadata_test.go +++ b/gopls/internal/regtest/workspace/metadata_test.go @@ -41,3 +41,63 @@ const C = 42 )) }) } + +// Test that moving ignoring a file via build constraints causes diagnostics to +// be resolved. +func TestIgnoreFile(t *testing.T) { + testenv.NeedsGo1Point(t, 16) // needs native overlays + + const src = ` +-- go.mod -- +module mod.test + +go 1.12 +-- foo.go -- +package main + +func main() {} +-- bar.go -- +package main + +func main() {} + ` + + WithOptions( + // TODO(golang/go#54180): we don't run in 'experimental' mode here, because + // with "experimentalUseInvalidMetadata", this test fails because the + // orphaned bar.go is diagnosed using stale metadata, and then not + // re-diagnosed when new metadata arrives. + // + // We could fix this by re-running diagnostics after a load, but should + // consider whether that is worthwhile. + Modes(Default), + ).Run(t, src, func(t *testing.T, env *Env) { + env.OpenFile("foo.go") + env.OpenFile("bar.go") + env.Await( + OnceMet( + env.DoneWithOpen(), + env.DiagnosticAtRegexp("foo.go", "func (main)"), + env.DiagnosticAtRegexp("bar.go", "func (main)"), + ), + ) + // Ignore bar.go. This should resolve diagnostics. + env.RegexpReplace("bar.go", "package main", "// +build ignore\n\npackage main") + + // To make this test pass with experimentalUseInvalidMetadata, we could make + // an arbitrary edit that invalidates the snapshot, at which point the + // orphaned diagnostics will be invalidated. + // + // But of course, this should not be necessary: we should invalidate stale + // information when fresh metadata arrives. + // env.RegexpReplace("foo.go", "package main", "package main // test") + env.Await( + OnceMet( + env.DoneWithChange(), + EmptyDiagnostics("foo.go"), + env.DiagnosticAtRegexpWithMessage("bar.go", "package (main)", "No packages"), + env.NoDiagnosticAtRegexp("bar.go", "func (main)"), + ), + ) + }) +}