gohugoio/hugo · error

could not find %q in the import context

Error message

could not find %q in the import context

What it means

Returned by the Dart Sass importResolver.Load when a URL prefixed with dartSassImportContextPrefix cannot be matched to a resource via resolveInImportContext. The resolver tries the import context, then partial-style name patterns; if none return a resource it errors. This means an @use/@import inside SCSS references a Hugo resource that does not exist in the import context.

Source

Thrown at resources/resource_transformers/tocss/dartsass/transform.go:248

	for _, namePattern := range namePatterns {
		if r := t.importContext.Get(path.Join(dir, fmt.Sprintf(namePattern, name))); r != nil {
			return r
		}
	}
	return nil
}

func (t importResolver) Load(url string) (godartsass.Import, error) {
	if subPath, ok := sass.HugoVarsSubPath(url); ok {
		return godartsass.Import{
			Content: sass.CreateVarsStyleSheet(sass.TranspilerDart, sass.ResolveVars(t.vars, subPath)),
		}, nil
	}

	if strings.HasPrefix(url, dartSassImportContextPrefix) {
		r := t.resolveInImportContext(url)
		if r == nil {
			return godartsass.Import{}, fmt.Errorf("could not find %q in the import context", url)
		}
		content, err := resources.InternalResourceSourceContent(t.ctx, r)
		return godartsass.Import{Content: content, SourceSyntax: sassSourceSyntax(url)}, err
	}

	filename, _ := paths.UrlStringToFilename(url)
	b, err := afero.ReadFile(hugofs.Os, filename)

	return godartsass.Import{Content: string(b), SourceSyntax: sassSourceSyntax(filename)}, err
}

func sassSourceSyntax(name string) godartsass.SourceSyntax {
	switch {
	case strings.HasSuffix(name, ".sass"):
		return godartsass.SourceSyntaxSASS
	case strings.HasSuffix(name, ".css"):
		return godartsass.SourceSyntaxCSS
	default:

View on GitHub (pinned to 52c9bd7908)

Solutions

  1. Verify the imported path resolves under assets/ relative to the importing file; for partials remember Hugo also checks the _prefixed name.
  2. Add the missing partial file to assets/ (with the correct _underscore convention) or fix the @use/@import URL.
  3. Check assets module mounts in hugo.toml to ensure the partial's directory is mounted into assets/.

Example fix

// before: main.scss
@use 'partials/buttons'; // _buttons.scss missing

// after
@use 'partials/buttons'; // assets/scss/partials/_buttons.scss now exists
Defensive patterns

Strategy: validation

Validate before calling

// Check that an @use/@import path resolves under assets before building.
func scssImportExists(assetsDir, rel string) bool {
    candidates := []string{rel, filepath.Join(filepath.Dir(rel), "_"+filepath.Base(rel))}
    for _, c := range candidates {
        for _, ext := range []string{".scss", ".sass", ".css"} {
            if _, err := os.Stat(filepath.Join(assetsDir, c+ext)); err == nil { return true }
        }
    }
    return false
}

Prevention

When it happens

Trigger: A .scss file processed by css.Sass (dartsass) contains @use 'partials/foo' or @import 'hugo-vars' where the referenced resource is not registered in the transformation's import context (e.g. not present in assets/ or not reachable relative to the entry).

Common situations: Renaming/moving a SCSS partial without updating @use paths, omitting the leading underscore for a partial that only exists as _foo.scss, case mismatch, or referencing a resource outside the assets mount.

Related errors


AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09). Data as JSON: /api/errors/1a4f1614ed2099d8. Report an issue: GitHub.