spotless icon indicating copy to clipboard operation
spotless copied to clipboard

Shebang Removal While Adding Copyright Header in Shell Scripts

Open usoni2210 opened this issue 1 year ago • 2 comments

  • [ ] summary of problem I’m working on a project with shell scripts and trying to add a copyright header that includes the year range from the Git history using the setLicenseHeaderYearsFromGitHistory property.

The issue I’m facing is that the shebang (#!/bin/bash, etc.) is getting removed from the script files in the process. It seems there's something wrong with the code flow image

  • [ ] Gradle or Maven version - Maven 3.8.8
  • [ ] spotless version - 2.43.0
  • [ ] operating system and version - MacOS 14
  • [ ] copy-paste your full Spotless configuration block(s), and a link to a public git repo that reproduces the problem if possible
<plugin>
  <groupId>com.diffplug.spotless</groupId>
  <artifactId>spotless-maven-plugin</artifactId>
  <version>2.43.0</version>
  <configuration>
    <setLicenseHeaderYearsFromGitHistory>true</setLicenseHeaderYearsFromGitHistory>
    <upToDateChecking>
      <enabled>false</enabled>
    </upToDateChecking>
    <java>
      <includes>
        <include>src/main/java/**/*.java</include>
        <include>src/test/java/**/*.java</include>
      </includes>
      <licenseHeader>
        <content>
          <![CDATA[
  /**
  ****************************************************************************
  **   Copyright Header
  ****************************************************************************
  **/]]>
        </content>
        <delimiter>package</delimiter>
      </licenseHeader>
    </java>
    <shell>
        <includes>
          <include>**/*.sh</include>
        </includes>
        <licenseHeader>
          <content>
            <![CDATA[
  #---------------------------------------------------------------------------
  #   Copyright Header
  #---------------------------------------------------------------------------]]>
          </content>
          <delimiter>## Start</delimiter>
          <skipLinesMatching>#\!/bin/sh</skipLinesMatching>
        </licenseHeader>
    </shell>
  </configuration>
</plugin>
  • [ ] copy-paste the full content of any console errors emitted by gradlew spotless[Apply/Check] --stacktrace - maven plugin

Thanks in advance

usoni2210 avatar Sep 24 '24 07:09 usoni2210

I suspect that maybe the shebang isn't being escaped into XML properly. I don't think you need the backslash, just #!/bin/sh should be fine.

nedtwigg avatar Oct 15 '24 18:10 nedtwigg

I suspect that maybe the shebang isn't being escaped into XML properly. I don't think you need the backslash, just #!/bin/sh should be fine.

I have tried that too but didn't work.

As I mentioned earlier, the issue lies within the code flow. when the setLicenseHeaderYearsFromGitHistory property is set, the code does not proceed to the step:format function to check for the shebang (in the else block of code screenshot). This results in the shebang being removed from the shell script files.

I’ve implemented a fix in my forked repository. You can view the changes here: https://github.com/usoni2210/spotless/compare/main...usoni2210:spotless:bugfix/2270

usoni2210 avatar Oct 17 '24 07:10 usoni2210

PRs welcome :)

nedtwigg avatar Oct 25 '24 16:10 nedtwigg