Why is there an Incorrect Package Declaration Message in the Script Editor?
Information about an Incorrect Package Declaration error message in the Script Editor that some users may encounter.
Warning introduced in version 7.1.0
In version 7.1.0, we introduced a warning message to the Script Editor to flag invalid/missing package declarations in script files. The message stated these invalid/missing package declarations wouldn't be supported from version 9.0.0 onwards. However, this is no longer the case. We will not do anything that breaks existing usage in future releases.
You will see a warning message (shown below) if you create, or have previously created, a script file under a directory without specifying a valid package declaration, something that we encourage in our best practices guide. This warning displays in Script Editor or when adding a script file to a feature configuration screen.
There is an additional bug that validates the last imported class in a script rather than the actual package declaration. This bug is due to be fixed in version 7.5.0.
Why we introduced a warning message
We recommended you use correct package declarations to prevent class name conflicts where you may have two files with the same name in different packages. For example, if you had foo/bar/file.groovy and abc/pqr/file.groovy without the package declaration, both would be loaded as file.class. Suppose you have the same named script in two different directories, and you don't have correct package declarations and import them into another script. In that case, you can get non-deterministic behaviour regarding which script is imported.
Warning to be updated in an upcoming release
We are in the process of updating the warning to an information message, as we will not remove support for invalid/missing package declarations from version 9.0.0. We suggest you use packages correctly for new scripts/classes, but there is no requirement to fix existing scripts, unless you want to. In the meantime we are looking at what tools we can provide, so it's easier to update existing scripts.
How do you get rid of this message?
Solution if there is no package declaration and the class isn't imported in any other script files
The solution is straightforward, and you must add the correct package declaration, as mentioned in the warning message at the top of the script.
For example, as mentioned in the screenshot above, adding package common should remove the warning message.
Solution if there is no package declaration and the class is imported in other script files
If you have imported this class in another script, in addition to adding the correct package declaration, you must rectify the import statement. For example, script files that import the class will show the error below.
As you can tell, if you had more than one file with the same name in different directories, it would not be possible to identify the correct class to load, which is one of the reasons for enforcing correct package declarations.
In this example, it is importcommon.util.
Solution if there is no package declaration and the directory structure is incorrect
Sometimes, the directory names may contain identifiers not valid in a package name. For example, int/util.groovy contains a keyword that can't be used as an identifier. In such cases, you will need to rename the directory structure so that it forms a valid package name.