Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog.d/check-skip-empty-targets.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Fixed

- Skip targets with no matching Go packages in `nsgo check -target` (#2267 by @J4ck1e2000).
48 changes: 36 additions & 12 deletions cmd/nsgo/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,18 @@ func vetArgs(jobs int, patterns ...string) []string {
return append(args, patterns...)
}

// checkTargets vets every requested target, up to jobs at once (one for 0). Only Apple mobile targets included through all may be skipped for an unavailable SDK; explicit names stay strict. A target's failure does not prevent checking the independent targets beside and after it; cancellation stops starting new ones.
const emptyVetReason = "no packages match ./... under the build constraints"

// goTool prefixes Go's diagnostics with the command that failed. Match
// the whole diagnostic so a package-load or vet error is never a skip.
const emptyVetError = `go vet failed
go: warning: "./..." matched no packages
no packages to vet`

// checkTargets vets the requested targets, up to jobs at once (one for 0).
// It skips empty package sets and, only for Apple mobile targets included
// through all, unavailable SDKs. Failures do not stop the other targets,
// but cancellation stops starting new ones.
func checkTargets(ctx context.Context, p *project, target string, jobs int) error {
requested := strings.Split(target, ",")
names, err := expandTargets(requested)
Expand All @@ -133,10 +144,8 @@ func checkTargets(ctx context.Context, p *project, target string, jobs int) erro
outcomes := make([]outcome, len(names))
slots := make(chan struct{}, max(1, min(jobs, len(names))))
var wg sync.WaitGroup
var interrupted error
for i, name := range names {
if ctx.Err() != nil {
interrupted = ctx.Err()
break
}
_, _, apple := appleOSOf(name)
Expand All @@ -150,7 +159,6 @@ func checkTargets(ctx context.Context, p *project, target string, jobs int) erro
case <-ctx.Done():
}
if ctx.Err() != nil {
interrupted = ctx.Err()
break
}
wg.Go(func() {
Expand All @@ -163,6 +171,11 @@ func checkTargets(ctx context.Context, p *project, target string, jobs int) erro
s := begin("vetting for %s", name)
if err == nil {
err = goToolWithTags(ctx, p, env, targetTags(name), vetArgs(jobs, "./...")...)
if err != nil && ctx.Err() == nil && err.Error() == emptyVetError {
s.fail()
outcomes[i].why = emptyVetReason
return
}
}
if err == nil && repository {
// The test hosts build only with nsgotest, which no app build sets, and each must implement testhost.Surface, so the repository vets them for every target it checks.
Expand Down Expand Up @@ -200,26 +213,37 @@ func checkTargets(ctx context.Context, p *project, target string, jobs int) erro
}
wg.Wait()
var skipped []string
// why is the reason the first target was skipped for, so a Mac whose Xcode lacks an SDK, or fails for another reason, says which.
// An SDK warning keeps the first lookup error as before, but empty
// package sets have their own reason. Report both in target order.
var why string
var failures []error
vetted := false
for i, o := range outcomes {
if o.why != "" {
skipped = append(skipped, names[i])
if why == "" {
switch {
case o.why != "":
if why != "" && (why == emptyVetReason) != (o.why == emptyVetReason) {
warn("skipped %s: %s", strings.Join(skipped, ", "), why)
skipped = skipped[:0]
}
if len(skipped) == 0 {
why = o.why
}
}
if o.err != nil {
skipped = append(skipped, names[i])
case o.err != nil:
failures = append(failures, o.err)
default:
vetted = true
}
}
if interrupted != nil {
failures = append(failures, interrupted)
if err := ctx.Err(); err != nil {
failures = append(failures, err)
}
if len(skipped) > 0 {
warn("skipped %s: %s", strings.Join(skipped, ", "), why)
}
if !vetted && len(failures) == 0 {
return errors.New("nothing was vetted: every requested target was skipped")
}
return errors.Join(failures...)
}

Expand Down
99 changes: 99 additions & 0 deletions cmd/nsgo/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,105 @@ func TestCheckTargetFailuresContinue(t *testing.T) {
}
}

func TestCheckTargetEmptyPackageSets(t *testing.T) {
// Use the runner for vet; the JSON policy needs no package scan when
// the user already picked an experiment.
t.Setenv("GOEXPERIMENT", "nojsonv2")
const noPackages = "go: warning: \"./...\" matched no packages\nno packages to vet\n"
tests := []struct {
name, targets string
linux, web string
wantError, neverSays string
wantLines []string
noXcode bool
}{
{
name: "mixed", targets: "linux,web", web: noPackages,
wantLines: []string{"vetted for linux_arm64", "vetted for linux_x64", "skipped web: no packages match ./... under the build constraints"},
neverSays: "vetted for web",
},
{
name: "all empty", targets: "web,linux", linux: noPackages, web: noPackages,
wantError: "nothing was vetted",
wantLines: []string{"skipped web, linux_arm64, linux_x64: no packages match ./... under the build constraints"},
neverSays: "vetted for ",
},
{
name: "additional diagnostic", targets: "web,linux_arm64",
web: noPackages + "broken.go:4:9: undefined: missing\n",
wantError: "web: go vet failed\n" + noPackages + "broken.go:4:9: undefined: missing",
wantLines: []string{"vetted for linux_arm64"}, neverSays: "skipped web",
},
{
name: "missing SDKs and empty web", targets: "web,all", web: noPackages, noXcode: true,
wantLines: []string{
"skipped web: no packages match ./... under the build constraints",
"skipped ios, ios_simulator, tvos, tvos_simulator, watchos, watchos_simulator:",
"Xcode",
},
neverSays: "vetted for web",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
p := checkFixture(t)
ctx := withProcessRunner(t, func(_ context.Context, cmd processCommand) ([]byte, error) {
switch {
case cmd.name == "xcrun" && tc.noXcode:
return nil, errors.New("Xcode SDK unavailable")
case cmd.name == "go" && len(cmd.args) == 2 && cmd.args[0] == "env" && cmd.args[1] == "GOFLAGS":
return nil, nil
case cmd.name == "go" && len(cmd.args) >= 2 && cmd.args[0] == "vet" && cmd.args[len(cmd.args)-1] == "./...":
var diagnostic string
switch envTarget(cmd.env) {
case "linux_arm64", "linux_x64":
diagnostic = tc.linux
case "js_wasm":
diagnostic = tc.web
}
if diagnostic != "" {
return []byte(diagnostic), errors.New("exit status 1")
}
return nil, nil
default:
return nil, fmt.Errorf("unexpected command %s %q", cmd.name, cmd.args)
}
})
var err error
out := captureStderr(t, func() { err = checkTargets(ctx, p, tc.targets, 0) })
if tc.wantError == "" && err != nil || tc.wantError != "" && (err == nil || !strings.Contains(err.Error(), tc.wantError)) {
t.Fatalf("check returned %v, wanted error %q:\n%s", err, tc.wantError, out)
}
rest := out
for _, want := range tc.wantLines {
_, after, ok := strings.Cut(rest, want)
if !ok {
t.Fatalf("missing or out of order %q:\n%s", want, out)
}
rest = after
}
if strings.Contains(out, tc.neverSays) {
t.Errorf("unexpected %q:\n%s", tc.neverSays, out)
}
})
}
}

func TestCheckTargetSkipsRealBuildConstraints(t *testing.T) {
if testing.Short() {
t.Skip("cross-vets a real package")
}
p := checkFixture(t)
writeFiles(t, p.Dir, map[string]string{
"main.go": "//go:build !js && !wasip1\n\npackage fixture\n\nvar Value = 1\n",
})
var err error
out := captureStderr(t, func() { err = check(t.Context(), p, "linux,web", 0) })
if err != nil || !strings.Contains(out, "vetted for linux_arm64") || !strings.Contains(out, "skipped web: no packages match ./...") {
t.Fatalf("check did not vet the Linux-only package and skip web: %v\n%s", err, out)
}
}

// A bare OS name vets that OS, as nsgo test -target spells it (#589).
func TestCheckBareOSTarget(t *testing.T) {
if testing.Short() {
Expand Down
8 changes: 7 additions & 1 deletion docs/architecture/tool.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,13 @@ NSH's packager supplies the packaging order; package YAML is NSGo's extension so

## Checking

`nsgo check` (`check.go`) checks the catalogs, runs `go vet ./...` and, for an app with a `tsconfig.json`, as only a TSX app has, writes the declarations of the modules its TSX imports to `.nsgo/types`, with `.nsgo/tsconfig.json`, and runs TypeScript 7's checker (`tsgo`, a pinned version of `github.com/microsoft/typescript-go` that `go run` builds and caches) on the app's `tsconfig.json`, which extends it. In NSGo's own repository it vets the root again with the `nsgodev` tag, and macOS, Windows, Linux and web dev builds with `nsgoautomation` too, tags only `nsgo dev` sets, so the dev client and the automation's agent are checked as well. A directory without `nsgo.yaml`, such as the repository's root, is checked as a tree of packages: catalogs and `go vet` alone. `-target` vets for each target named instead of the host and the dev client, in the environment its build sets (`GOOS`, `GOARCH`, cgo with Xcode's clang for iOS), so code behind another platform's build tags is checked from a Mac too, and a run for some targets repeats no plain check's work. In NSGo's repository each target also vets `internal/platform/...` with `nsgotest`, checking its test hosts against `testhost.Surface`; HarmonyOS adds `nsgoharmony`, as its app builds do. `all` is `build`'s list of what a Mac with Go and Xcode builds, but `check` skips its Apple mobile targets off macOS, or on a Mac whose SDK or clang lookup for one fails, such as an Xcode without the watchOS platform, and names the skipped targets and the first reason in one warning; a target named on its own, even beside `all`, stays required, and `build -target all` still needs Xcode. `-jobs n` vets up to n targets at once and passes `-p n` to each `go vet`, so one check bounds the compilers it runs, n² at most; without it targets go one at a time with go's own `-p`. A target whose vet fails does not stop the others: their errors are returned together in the targets' order, and cancellation starts no more. A check holds the operating system's lock on `.nsgo/check.lock` at its module's root (`lockCheck`; `flock`, or `LockFileEx` on Windows), so a second check in the module names the first's pid and waits, and a killed check's lock goes with its process. The declarations come from the app's own program: the tool builds its main package with a file, added through the overlay, whose `init` runs after the package's others, so after every `tsx.Register`, writes them and exits before `main`. It checks with `go list` that the file is in the package before it builds, so the app itself never runs. A directory with `nsgo.yaml` but no Go, such as the TSX app a package holds, has nothing to vet, and its declarations are those of every module NSGo gives, from a main package the tool writes in `.nsgo/typesmain` importing them all, since the host decides which its packages may import. `nsgo dev` writes them too, for editors, after each Go build of an app with a `tsconfig.json` once the app has started; a failure only warns.
`nsgo check` (`check.go`) checks the catalogs, runs `go vet ./...` and, for an app with a `tsconfig.json`, as only a TSX app has, writes the declarations of the modules its TSX imports to `.nsgo/types`, with `.nsgo/tsconfig.json`, and runs TypeScript 7's checker (`tsgo`, a pinned version of `github.com/microsoft/typescript-go` that `go run` builds and caches) on the app's `tsconfig.json`, which extends it. In NSGo's own repository it vets the root again with the `nsgodev` tag, and macOS, Windows, Linux and web dev builds with `nsgoautomation` too, tags only `nsgo dev` sets, so the dev client and the automation's agent are checked as well. A directory without `nsgo.yaml`, such as the repository's root, is checked as a tree of packages: catalogs and `go vet` alone.

`-target` vets the expanded target names instead of the host and the dev client, in the environment each build sets (`GOOS`, `GOARCH`, cgo with Xcode's clang for iOS). The primary `go vet ./...` skips a target only on Go's complete unmatched-pattern diagnostic; host vet and the repository's additional vets still treat it as a failure. In NSGo's repository each target also vets `internal/platform/...` with `nsgotest`, checking its test hosts against `testhost.Surface`; HarmonyOS adds `nsgoharmony`, as its app builds do. `all` is `build`'s list of what a Mac with Go and Xcode builds, but `check` skips its Apple mobile targets off macOS, or on a Mac whose SDK or clang lookup for one fails, such as an Xcode without the watchOS platform. An explicitly named Apple target, even beside `all`, still requires its SDK, and `build -target all` still needs Xcode.

The tool reports empty-package and unavailable-SDK skips separately in target order, retaining the first lookup error in an SDK group. If every target is skipped, the command fails because nothing was vetted. `-jobs n` vets up to n targets at once and passes `-p n` to each `go vet`, so one check bounds the compilers it runs, n² at most; without it targets go one at a time with go's own `-p`. A target whose vet fails does not stop the others: their errors are returned together in the targets' order, and cancellation starts no more. A check holds the operating system's lock on `.nsgo/check.lock` at its module's root (`lockCheck`; `flock`, or `LockFileEx` on Windows), so a second check in the module names the first's pid and waits, and a killed check's lock goes with its process.

The declarations come from the app's own program: the tool builds its main package with a file, added through the overlay, whose `init` runs after the package's others, so after every `tsx.Register`, writes them and exits before `main`. It checks with `go list` that the file is in the package before it builds, so the app itself never runs. A directory with `nsgo.yaml` but no Go, such as the TSX app a package holds, has nothing to vet, and its declarations are those of every module NSGo gives, from a main package the tool writes in `.nsgo/typesmain` importing them all, since the host decides which its packages may import. `nsgo dev` writes them too, for editors, after each Go build of an app with a `tsconfig.json` once the app has started; a failure only warns.

Go vet and tests, headless or on a target, follow the build's JSON policy (`smallJSON` in `build.go`). The tool adds `nojsonv2` to the experiments reported by `go env GOEXPERIMENT` unless that setting, from the environment or `go env -w`, already names `jsonv2` or `nojsonv2`, or the command's packages or dependencies import `encoding/json/v2` or `encoding/json/jsontext`. Test imports count when vetting or building tests. The first `encoding/json` implementation is already the default in Go 1.26; on Go 1.27 it links about 1 MB less on the web, 450 KB less on Android and 100 to 450 KB less on desktop targets. Mini program builds and target checks always use the first implementation to fit the package limit.

Expand Down
2 changes: 2 additions & 0 deletions docs/public/en/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ go tool nsgo test -bench . ./core # core's benchmarks

`-target` vets the targets it names instead of the host and, in NSGo's repository, the dev client, which the plain `check` vets. To vet the platforms a change touches, name them all in one check, such as `check -target web,android -jobs 2`, rather than starting several: `-jobs n` vets up to n targets at once, each `go vet` with n compile workers, where the default is one target at a time with Go's default, and a check started while another runs in the same module waits for it and says so. CI's `check.yml` vets every target.

Reports use expanded target names: `linux`, for example, becomes `linux_arm64` and `linux_x64`. If a target's build constraints leave no packages matching `./...`, `check` lists that target as skipped with the reason and vets the other targets. If every target is skipped, the command fails because nothing was vetted.

Run at the repository root, or in `internal/dev`, the headless command tests `internal/dev` a second time with the `nsgodev` build tag, which the dev client's files need, so its tests run locally and in CI. Both runs take the same filters and flags and share one tally and JUnit report, where the second run's package is named `internal/dev [nsgodev]`.

`check -target all` vets the macOS, Windows, Linux, Android arm64 and web targets on any host. It also vets iOS, tvOS and watchOS when their Xcode SDKs are available, and lists the ones it skips. An explicitly named target, such as `check -target ios`, fails if its tools are unavailable, including when combined with `all`. A failed target does not prevent checking the remaining targets, and the command fails if any required check fails. `build -target all` still needs macOS with Xcode. In NSGo's own repository, each named target also vets its platform test hosts with `nsgotest`; CI names the preview `harmonyos` target explicitly, since it is outside `all`.
Expand Down
2 changes: 2 additions & 0 deletions docs/public/zh/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ go tool nsgo test -bench . ./core # core 的基准测试

`-target` 只检查它指定的目标,不再检查宿主系统,在 NSGo 自己的仓库里也不检查 dev client,这些由不带参数的 `check` 检查。要检查一项改动涉及的平台,把它们都写进同一个检查,例如 `check -target web,android -jobs 2`,不要同时启动多个:`-jobs n` 最多同时检查 n 个目标,每次 `go vet` 使用 n 个编译进程,默认一次检查一个目标并使用 Go 的默认值;同一模块里已有检查在运行时,后启动的检查会提示并等待它结束。CI 的 `check.yml` 会检查所有目标。

检查结果使用展开后的目标名称,例如 `linux` 会展开为 `linux_arm64` 和 `linux_x64`。如果某个目标的构建约束使 `./...` 匹配不到任何包,`check` 会列出被跳过的目标及原因,并继续检查其余目标。如果所有目标都被跳过,命令会失败并说明没有检查任何包。

在仓库根目录或 `internal/dev` 中运行无头测试命令时,它会带上 `nsgodev` 构建标签再测试一次 `internal/dev`(dev client 的文件需要这个标签),让本地和 CI 都运行 dev client 的测试。两次运行使用相同的筛选条件和参数,结果合并到同一份统计和 JUnit 报告中,第二次运行的包名为 `internal/dev [nsgodev]`。

`check -target all` 在任何宿主系统上都会检查 macOS、Windows、Linux、Android arm64 和 Web 目标。有对应 Xcode SDK 时,还会检查 iOS、tvOS 和 watchOS,并集中列出跳过的目标。显式指定的目标(例如 `check -target ios`)在所需工具不可用时仍会失败,与 `all` 一起指定时也一样。一个目标失败不会阻止后续目标的检查,任何必需检查失败都会让命令失败。`build -target all` 仍需要装有 Xcode 的 macOS。在 NSGo 自己的仓库里,每个指定的目标还会带 `nsgotest` 检查平台的测试宿主;`harmonyos` 预览目标不在 `all` 里,CI 会单独指定它。
Expand Down
2 changes: 1 addition & 1 deletion skills/nsgo/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ go tool nsgo test # headless; -run as go test's, -target macos|

Vetting and tests use the same Go JSON policy as `build`. Headless `nsgo test -- -c` compiles tests without running them; compiler failures remain visible in the console and JUnit report.

Name every target a change touches in one `check -target`, with `-jobs n` to vet up to n at once, each `go vet` with n compile workers; never start several checks, as a check waits for the one already running in its module.
Name every target a change touches in one `check -target`, with `-jobs n` to vet up to n at once, each `go vet` with n compile workers; never start several checks, as a check waits for the one already running in its module. Reports name expanded targets (`linux_arm64`, `linux_x64` for `linux`). A target with no matching Go packages is skipped with a reason; if every target is skipped, the check fails.

Test an app as a user uses it with `nsgotest`, finding views by text, role, label or test ID. Read [references/testing.md](references/testing.md) before writing tests.

Expand Down
Loading