Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

License check breaks if file has no package declaration #135

Closed
chrisbanes opened this issue Aug 24, 2017 · 8 comments
Closed

License check breaks if file has no package declaration #135

chrisbanes opened this issue Aug 24, 2017 · 8 comments
Labels

Comments

@chrisbanes
Copy link

I got the following exception:

Step 'licenseHeader' found problem in 'src/main/java/me/banes/chris/tivi/extensions/PreferenceExtensions.kt':
Unable to find delimiter regex ^package 
java.lang.IllegalArgumentException: Unable to find delimiter regex ^package 
        at com.diffplug.spotless.generic.LicenseHeaderStep.format(LicenseHeaderStep.java:88)
        at com.diffplug.spotless.FormatterStepImpl$Standard.format(FormatterStepImpl.java:78)
        at com.diffplug.spotless.FormatterStep$Strict.format(FormatterStep.java:76)
        at com.diffplug.spotless.Formatter.compute(Formatter.java:230)
        at com.diffplug.spotless.Formatter.isClean(Formatter.java:167)
        at com.diffplug.gradle.spotless.SpotlessTask.check(SpotlessTask.java:216)
        at com.diffplug.gradle.spotless.SpotlessTask.performAction(SpotlessTask.java:172)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:73)
@jbduncan
Copy link
Member

jbduncan commented Aug 24, 2017

@chrisbanes I think I understand the cause of your exception.

IIUC, Spotless needs a way of identifying what counts as the 'top' of a source file so that it can reliably put the specified license header just above. By default, we look for the substring "package" to identify the top of Kotlin files. However, it seems in your case that you have a Kotlin file that doesn't start with "package", so Spotless cannot find the 'top' of the file.

It occurs to me that we don't provide a licenseHeader(String, String) overload for Kotlin files that would allow one to swap out the 'top' of a given Kotlin file with a given substring instead of "package"...

@jbduncan
Copy link
Member

Note: I've just edited my previous comment for clarity.

@nedtwigg
Copy link
Member

nedtwigg commented Aug 24, 2017

We do provide a way to specify the start of the file manually. The easiest workaround is to avoid the default package. If you don't want to change that, then you have to provide the regex yourself, e.g. something like licenseHeaderFile('license.txt', '^(package|//startfile)') if you want to use the //startfile comment to signify the start of the file. Without a package declaration, it's very difficult to find a regex that will work in the general case. Files might start with imports, comments, a class declaration, etc.

@jbduncan
Copy link
Member

We do provide a way to specify the start of the file manually.

Oops, I missed that. Thanks for correcting me @nedtwigg. 😜

@chrisbanes
Copy link
Author

The default package is used a lot more in Kotlin files, since you can have extensions methods, etc. Thanks though, licenseHeaderFile('license.txt', '^(package|//startfile)') works well.

@nedtwigg
Copy link
Member

Is it required to use the default package in Kotlin, or just more convenient? If it's required, maybe we should update our docs to include this tip.

@chrisbanes
Copy link
Author

It's more convenience

@nedtwigg
Copy link
Member

Roger. I'll close this for now. If enough future users bump into this same issue, maybe we'll put the //startfile workaround into the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants