Invalid Package Declaration
ID |
kotlin.invalid_package_declaration |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Kotlin |
Tags |
naming, reliability |
Description
Reports a package declaration whose dotted path does not align with the
file’s parent directory structure. By Kotlin convention a file at
<root>/foo/bar/Baz.kt should declare package …foo.bar. When the
declared package and the on-disk path drift apart, IDE navigation, build
tooling, and code readers all pay the cost.
Rationale
// File: src/.../foo/bar/Baz.kt
package com.example.foo.bar // OK — ends in `foo.bar`
package com.example.wrong // FLAW — does not end in `foo.bar`
Mismatched packages let two declarations with the same fully-qualified name land in different files, break "Go to declaration", and confuse refactoring tooling that relies on the file-system layout matching the package graph.
Remediation
Either rename the package to match the directory, or move the file into a directory tree that matches the package. Most IDEs offer a single refactoring action that does either consistently.