{"record":{"id":"2890dbca5f177344","repo":"grafana/k6","slug":"error-while-parsing-use-directives-constraint-q-f","errorCode":null,"errorMessage":"error while parsing use directives constraint %q for %q in %q: %w","messagePattern":"error while parsing use directives constraint %q for %q in %q: %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/cmd/launcher.go","lineNumber":406,"sourceCode":"\tfor _, directive := range directives {\n\t\t// normalize spaces\n\t\tdirective = strings.ReplaceAll(directive, \"  \", \" \")\n\t\tif !strings.HasPrefix(directive, \"use k6\") {\n\t\t\tcontinue\n\t\t}\n\t\tdirective = strings.TrimSpace(strings.TrimPrefix(directive, \"use k6\"))\n\t\tdep := \"k6\"\n\t\tconstraint := directive\n\t\tif strings.HasPrefix(directive, \"with k6/x/\") {\n\t\t\tdirective = strings.TrimSpace(strings.TrimPrefix(directive, \"with \"))\n\t\t\tdep, constraint, _ = strings.Cut(directive, \" \")\n\t\t}\n\t\tvar con *semver.Constraints\n\t\tvar err error\n\t\tif len(constraint) > 0 {\n\t\t\tcon, err = semver.NewConstraint(constraint)\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"error while parsing use directives constraint %q for %q in %q: %w\", constraint, dep, name, err)\n\t\t\t}\n\t\t}\n\n\t\terr = deps.update(dep, con)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"error while parsing use directives in %q: %w\", name, err)\n\t\t}\n\t}\n\n\treturn nil\n}\n\nfunc findDirectives(text []byte) []string {\n\t// parse #! at beginning of file\n\tif bytes.HasPrefix(text, []byte(\"#!\")) {\n\t\t_, text, _ = bytes.Cut(text, []byte(\"\\n\"))\n\t}\n","sourceCodeStart":388,"sourceCodeEnd":424,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/cmd/launcher.go#L388-L424","documentation":"Produced while processing `use k6 ...` version directives found in the script and its imported files (the launcher parses lines like `use k6 >= v0.50` or `use k6 with k6/x/example v0.3.0`). After cutting off the dependency name, any remaining text is fed to semver.NewConstraint, and text that is not a valid semver constraint range produces this error, which includes the bad constraint, the dependency, and the file where the directive was found.","triggerScenarios":"A directive such as `use k6 v50` (missing minor/patch), `use k6 => latest`, `use k6 with k6/x/foo == 1` (incomplete comparator), or stray text after `use k6` like `use k6 and above`; also copy-pasting npm-style selectors (e.g. `^`, `~` are fine but `latest`, `next`, or bare `>` with no version are not).","commonSituations":"Teams migrating from npm-style version pinning habits; LLM/snippet-sourced scripts with plausible-but-invalid directives; typos like `>=v0.5.0 ` with invisible trailing characters; multiple scripts where one import's header directive is malformed.","solutions":["Read the error: it quotes the exact bad constraint, the dependency, and the file — open that file's directive line","Rewrite the constraint as a valid semver range: `use k6 >= v0.50.0`, `use k6 v0.50.0`, or `use k6 with k6/x/sql 0.x`","Avoid bare words like latest/next — pin an explicit version or range","Re-run; if multiple directives exist, fix them one at a time (each parse failure reports its file)"],"exampleFix":"// before (script.js)\nuse k6 latest\nexport default function () {}\n// error: error while parsing use directives constraint \"latest\" for \"k6\" in \"script.js\"\n\n// after\nuse k6 >= v0.56.0\nexport default function () {}","handlingStrategy":"validation","validationCode":"# Lint all use-directives against semver range syntax before running\nnode -e '\nconst {validRange} = require(\"semver\");\nconst fs = require(\"fs\");\nfor (const f of process.argv.slice(1)) {\n  const m = fs.readFileSync(f, \"utf8\").match(/^use\\s+k6.*$/gm) || [];\n  for (const line of m) {\n    const c = line.replace(/^use\\s+k6(\\s+with)?/, \"\").trim().split(/\\s+/).pop();\n    if (c && !validRange(c)) { console.error(`bad constraint ${c} in ${f}`); process.exitCode = 1; }\n  }\n}' main.js lib/*.js","typeGuard":"function isValidK6UseConstraint(c) {\n  // cheap check: comparator(optional) + dotted version, e.g. \">=v0.56.0\", \"0.x\", \"v1.2.3\"\n  return /^(>=|<=|>|<|=|~|\\^)?\\s*v?\\d+(\\.\\d+|\\.x)+(\\s+-\\s+(>=|<=|>|<|=|~|\\^)?\\s*v?\\d+(\\.\\d+|\\.x)+)?$/.test(c.trim());\n}","tryCatchPattern":null,"preventionTips":["Pin explicit ranges like `use k6 >= v0.56.0`; never words like latest","Add a lint step for `^use ` lines in CI for all JS files, including imported helpers","Keep a repo-wide single source of truth for the k6 version constraint"],"tags":["semver","directives","script","parsing"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}