Skip to content

Commit c17dcc3

Browse files
authored
Fix failing lints on source files (#1049)
This PR updates the toolkit testing to: * Determine whether to lint as a module or source file based on the presence of any `export ` line in the file. * Run `nu-check` on files before linting with `use <file>` or `source <file>` * Updates the environment variable to `TEST_METHOD` with options for `ide-check` or `import-or-source`. * Updates the default to `import-or-source` (was `ide-check`) to match CI * Removes environment variable from CI since this test method is now the default. With this in place we should have far fewer (false positive) failing CI runs.
1 parent 446f06f commit c17dcc3

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ jobs:
2424
# nix STUB_IDE_CHECK when nushell/nushell#12208 fixed
2525
run: |
2626
use ${{ github.workspace }}/toolkit.nu *
27-
STUB_IDE_CHECK=true check pr --and-exit
27+
check pr --and-exit

toolkit.nu

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,43 @@ def "with files" [
6666
}
6767
}
6868

69-
# Check the input file with nu --ide-check.
70-
export def "lint ide-check" []: path -> int {
69+
export def "lint check" []: path -> int {
7170
let file = $in
72-
let stub = $env.STUB_IDE_CHECK? | default false | into bool
71+
let test_methodology = $env.TEST_METHOD? | default "import-or-source"
7372
const current_path = (path self)
74-
let diagnostics = if $stub {
75-
do { nu --no-config-file --commands $"use '($file)'" }
76-
| complete
77-
| [[severity message]; [$in.exit_code $in.stderr]]
78-
| where severity != 0
79-
} else {
80-
nu --ide-check 10 $file
73+
74+
let diagnostics = match $test_methodology {
75+
ide-check => {
76+
nu --ide-check 10 $file
8177
| $"[($in)]"
8278
| from nuon
8379
| where type == diagnostic
8480
| select severity message
81+
}
82+
83+
import-or-source => {
84+
# If any line in the file starts with `export`, then
85+
# we assume it is a module. Otherwise, treat it as source
86+
let has_exports = (open $file | $in like '(?m)^export\s')
87+
if $has_exports {
88+
# treat as module
89+
if not (nu-check --as-module $file) {
90+
do { nu --no-config-file --commands $"use '($file)'" }
91+
| complete
92+
| [[severity message]; [$in.exit_code $in.stderr]]
93+
| where severity != 0
94+
}
95+
} else {
96+
if not (nu-check $file) {
97+
do { nu --no-config-file --commands $"source '($file)'" }
98+
| complete
99+
| [[severity message]; [$in.exit_code $in.stderr]]
100+
| where severity != 0
101+
}
102+
}
103+
}
104+
105+
_ => { error make { msg: "Invalid TEST_METHOD"}}
85106
}
86107
let error_count = $diagnostics | length
87108
if $error_count == 0 {
@@ -101,6 +122,6 @@ export def lint [
101122
--and-exit # Exit with error count
102123
]: [list<path> -> int, nothing -> int] {
103124
with files --full=$full --and-exit=$and_exit {
104-
par-each { lint ide-check }
125+
par-each { lint check }
105126
}
106127
}

0 commit comments

Comments
 (0)