Skip to content

origin() call crashes with postcss 8.5.16: Column must be greater than or equal to 0, got -1 #320

Description

@chida09

Summary

Since postcss 8.5.16 (postcss/postcss#2036), Input#origin() expects 1-based columns and internally converts to 0-based before passing to source-map-js.

However, happy-css-modules still applies its own - 1 workaround in src/locator/postcss.ts#L95-L99:

const classSelectorOrigin = rule.source.input.origin(
    classSelectorLocation.start.line,
    // The column of `Input#origin` is 0-based. This behavior is undocumented and probably a postcss's bug.
    // TODO: Open PR to postcss/postcss
    classSelectorLocation.start.column - 1,
);

This results in double subtraction (column - 1 here + column - 1 inside postcss), producing -1 when the column is 1, which causes source-map-js to throw:

TypeError: Column must be greater than or equal to 0, got -1

Suggested fix

Remove the - 1 workaround since postcss 8.5.16 now correctly handles the conversion:

 const classSelectorOrigin = rule.source.input.origin(
     classSelectorLocation.start.line,
-    // The column of `Input#origin` is 0-based. This behavior is undocumented and probably a postcss's bug.
-    // TODO: Open PR to postcss/postcss
-    classSelectorLocation.start.column - 1,
+    classSelectorLocation.start.column,
 );

Reproduction

# With postcss 8.5.16 and happy-css-modules 5.0.1
hcm '**/*.module.scss'
# => AggregateError: Failed to process files
# => TypeError: Column must be greater than or equal to 0, got -1

Downgrading to postcss 8.5.15 resolves the issue.

Environment

  • happy-css-modules: 5.0.1
  • postcss: 8.5.16
  • source-map-js: 1.2.1
  • sass: 1.101.0

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions