grafana/k6 · error

error while parsing use directives constraint

Error message

error while parsing use directives constraint %q for %q in %q: %w

What it means

Wrapped error from processUseDirectives while parsing a 'use k6 >=v0.50' (or 'use k6 with k6/x/ext constraint') directive in a script: the semver constraint string itself is invalid and could not be compiled by semver.NewConstraint.

Solutions

  1. Use valid semver constraint syntax, e.g. '>= v0.50.0', '~0.53'
  2. Check for typos in the version constraint after 'use k6' or 'with k6/x/...'
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at internal/cmd/launcher.go:406 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of grafana/k6@8d06114777 (2026-08-18). Data as JSON: /api/errors/2890dbca5f177344. Report an issue: GitHub.

Appendix: source

Thrown at internal/cmd/launcher.go:405

	for _, directive := range directives {
		// normalize spaces
		directive = strings.ReplaceAll(directive, "  ", " ")
		if !strings.HasPrefix(directive, "use k6") {
			continue
		}
		directive = strings.TrimSpace(strings.TrimPrefix(directive, "use k6"))
		dep := "k6"
		constraint := directive
		if strings.HasPrefix(directive, "with k6/x/") {
			directive = strings.TrimSpace(strings.TrimPrefix(directive, "with "))
			dep, constraint, _ = strings.Cut(directive, " ")
		}
		var con *semver.Constraints
		var err error
		if len(constraint) > 0 {
			con, err = semver.NewConstraint(constraint)
			if err != nil {
				return fmt.Errorf("error while parsing use directives constraint %q for %q in %q: %w", constraint, dep, name, err)
			}
		}

		err = deps.update(dep, con)
		if err != nil {
			return fmt.Errorf("error while parsing use directives in %q: %w", name, err)
		}
	}

	return nil
}

func findDirectives(text []byte) []string {
	// parse #! at beginning of file
	if bytes.HasPrefix(text, []byte("#!")) {
		_, text, _ = bytes.Cut(text, []byte("\n"))
	}

View on GitHub (pinned to 8d06114777)