commitlint icon indicating copy to clipboard operation
commitlint copied to clipboard

fix: published docker image does not behave consistently with examples from documentation

Open naffers opened this issue 11 months ago • 7 comments

Steps to Reproduce

  1. docker pull commitlint/commitlint:latest

  2. Run the container providing a string to lint: echo "foo" | docker run -i commitlint/commitlint:latest Result:

    echo "foo" | docker run -i commitlint/commitlint:latest
    â§—   input: foo
    ✖   Please add rules to your `commitlint.config.js`
        - Getting started guide: https://commitlint.js.org/guides/getting-started
        - Example config: https://github.com/conventional-changelog/commitlint/blob/master/%40commitlint/config-conventional/src/index.ts [empty-rules]
    
    ✖   found 1 problems, 0 warnings
    ⓘ   Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint
    
  3. Extending @commitlint/config-conventional in a file named commitlint.config.js Okay so I add the simplest config:

    export default {
      extends: [
        '@commitlint/config-conventional'
      ]
    }
    
  4. Then running the command but additionally volume mount this config into the container: echo "foo" | docker run -i -v $(pwd)/commitlint.config.js:/commitlint.config.js commitlint/commitlint:latest Result:

    export default {
    ^^^^^^
    
    SyntaxError: Unexpected token 'export'
    
  5. Switch the configuration to CJS format:

    module.exports = {
      extends: [
        '@commitlint/config-conventional'
      ]
    }
    
  6. Run the same command: echo "foo" | docker run -i -v $(pwd)/commitlint.config.js:/commitlint.config.js commitlint/commitlint:latest Result:

    â§—   input: foo
    ✖   subject may not be empty [subject-empty]
    ✖   type may not be empty [type-empty]
    
    ✖   found 2 problems, 0 warnings
    ⓘ   Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint
    

    Which indicates that configuration style does work.

  7. Additionally I've noticed that if you use another allowable config file name such as .commitlintrc rename the above working file to this. Change the docker run command to reference the new filename: echo "foo" | docker run -i -v $(pwd)/.commitlintrc:/.commitlintrc commitlint/commitlint:latest

    Result: You get a massive YAMLException: YAML Error in /.commitlintrc

Current Behavior

No response

Expected Behavior

Reading through the documentation I maybe naively expected this would behave as the locally installed version, just being ran within a container.

As highlighted above it is my experience that it has some quite different behaviour whether intentional or not.

Is the difference intentional, or should the pre-built Dockerfile behave as if you had the npm packages installed locally?

Affected packages

  • [ ] cli
  • [ ] core
  • [ ] prompt
  • [ ] config-angular

Possible Solution

No response

Context

I am attempting to integrate a commitlint job within GitLab CI but the errors I've highlighted tripped me up and led me to investigate this in a bit more detail outside of that context.

I wanted to understand whether this was intentional, I'm doing something obviously wrong, or actually whether it is a bug and so I'm raising this to potentially highlight a yet undiscovered issue.

commitlint --version

19.6.0

git --version

2.47.1

node --version

18.20.5

naffers avatar Jan 02 '25 16:01 naffers

Thanks, is this only happening using docker or is this also happening with using i.e. npm/npx?

escapedcat avatar Jan 03 '25 09:01 escapedcat

Apologies for the delay in responding.

So following the setup here outside of a docker context will result in a failure related to:

export default { extends: ['@commitlint/config-conventional'] };
^^^^^^

SyntaxError: Unexpected token 'export'

Switching to CJS format as I did in point 5. fixes the issue here, but also as I've got my package.json in this context I'm able to switch it to "type": "module" without changing to CJS to resolve this too.

However as I mention in the original description point 7. the same occurs here in this context.

Renaming the configuration to .commitlintrc whether using CJS or ESM module syntax results in a YAMLexception

file:///D:/commitlint-test/node_modules/@commitlint/cli/lib/cli.js:132
        throw err;
        ^
YAMLException: YAML Error in D:\commitlint-test\.commitlintrc:
bad indentation of a mapping entry (1:65)

 1 |  ... mmitlint/config-conventional'] };

Making me think there's some internal difference to how it handles different configuration file names which isn't called out in the documentation. Or it's bug if they should all be handled the same way?

naffers avatar Jan 06 '25 09:01 naffers

/cc @jeohist did you experience anything like this?

escapedcat avatar Jan 06 '25 09:01 escapedcat

/cc @jeohist did you experience anything like this?

No, never experienced anything like this.

jeohist avatar Jan 06 '25 10:01 jeohist

Same issue here using the latest commitlint Docker image:

/ $ echo "export default { extends: ['@commitlint/config-conventional'] };" > commitlint.config.js
/ $ echo test | commitlint
/commitlint.config.js:1
export default { extends: ['@commitlint/config-conventional'] };
^^^^^^

SyntaxError: Unexpected token 'export'
    at internalCompileFunction (node:internal/vm:76:18)
    at wrapSafe (node:internal/modules/cjs/loader:1283:20)
    at Module._compile (node:internal/modules/cjs/loader:1328:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1422:10)
    at Module.load (node:internal/modules/cjs/loader:1203:32)
    at Module._load (node:internal/modules/cjs/loader:1019:12)
    at Module.require (node:internal/modules/cjs/loader:1231:19)
    at module.exports (/usr/local/lib/node_modules/@commitlint/load/node_modules/import-fresh/index.js:33:91)
    at loadJsSync (/usr/local/lib/node_modules/@commitlint/load/node_modules/cosmiconfig/dist/loaders.js:17:12)
    at #loadConfiguration (/usr/local/lib/node_modules/@commitlint/load/node_modules/cosmiconfig/dist/Explorer.js:116:42) {
  filepath: '/commitlint.config.js'
}

Node.js v18.20.6

Fixed it by using the .mjs extension when creating the configuration file to explicitly use ES Module Syntax:

/ $ echo "export default { extends: ['@commitlint/config-conventional'] };" > commitlint.config.mjs
/ $ echo test | commitlint
â§—   input: test
✖   subject may not be empty [subject-empty]
✖   type may not be empty [type-empty]

✖   found 2 problems, 0 warnings
ⓘ   Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint

Another alternative is keeping the .js extension and use the CommonJS syntax:

/ $ echo "module.exports = { extends: ['@commitlint/config-conventional'] };" > commitlint.config.js
/ $ echo test | commitlint
â§—   input: test
✖   subject may not be empty [subject-empty]
✖   type may not be empty [type-empty]

✖   found 2 problems, 0 warnings
ⓘ   Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint

m4dh4t avatar Feb 06 '25 07:02 m4dh4t

Apologies for the delay in responding.

So following the setup here outside of a docker context will result in a failure related to:

export default { extends: ['@commitlint/config-conventional'] };
^^^^^^

SyntaxError: Unexpected token 'export'

Switching to CJS format as I did in point 5. fixes the issue here, but also as I've got my package.json in this context I'm able to switch it to "type": "module" without changing to CJS to resolve this too.

However as I mention in the original description point 7. the same occurs here in this context.

Renaming the configuration to .commitlintrc whether using CJS or ESM module syntax results in a YAMLexception

file:///D:/commitlint-test/node_modules/@commitlint/cli/lib/cli.js:132
        throw err;
        ^
YAMLException: YAML Error in D:\commitlint-test\.commitlintrc:
bad indentation of a mapping entry (1:65)

 1 |  ... mmitlint/config-conventional'] };

Making me think there's some internal difference to how it handles different configuration file names which isn't called out in the documentation. Or it's bug if they should all be handled the same way?

I met the same issue by follwing the steps in https://commitlint.js.org/guides/getting-started.html

jiaxshi avatar May 12 '25 09:05 jiaxshi

@jiaxshi this issue is about the docker image.
Sounds like your issue is with the normal usage? Yes? If so, please open a new issue thanks.

escapedcat avatar May 12 '25 09:05 escapedcat