Skip to content

Commit

Permalink
Update resolve.go to provide more human-friendly error output
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-torok committed Aug 19, 2024
1 parent 13ce192 commit 5b7e8cf
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 12 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ A brief description of the categories of changes:
[x.x.x]: https://github.com/bazelbuild/rules_python/releases/tag/x.x.x

### Changed
* Nothing yet
* (gazelle): Update error messages when unable to resolve a dependency to be more human-friendly.

### Added
* Nothing yet
Expand Down
17 changes: 9 additions & 8 deletions gazelle/python/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,11 @@ func (py *Resolver) Resolve(
continue MODULES_LOOP
} else if cfg.ValidateImportStatements() {
err := fmt.Errorf(
"%[1]q at line %[2]d from %[3]q is an invalid dependency: possible solutions:\n"+
"%[1]q, line %[2]d: %[3]q is an invalid dependency: possible solutions:\n"+
"\t1. Add it as a dependency in the requirements.txt file.\n"+
"\t2. Instruct Gazelle to resolve to a known dependency using the gazelle:resolve directive.\n"+
"\t3. Ignore it with a comment '# gazelle:ignore %[1]s' in the Python file.\n",
moduleName, mod.LineNumber, mod.Filepath,
"\t2. Use the '# gazelle:resolve py %[3]s TARGET_LABEL' BUILD file directive to resolve to a known dependency.\n"+
"\t3. Ignore it with a comment '# gazelle:ignore %[3]s' in the Python file.\n",
mod.Filepath, mod.LineNumber, moduleName,
)
errs = append(errs, err)
continue POSSIBLE_MODULE_LOOP
Expand All @@ -236,9 +236,10 @@ func (py *Resolver) Resolve(
}
if len(sameRootMatches) != 1 {
err := fmt.Errorf(
"multiple targets (%s) may be imported with %q at line %d in %q "+
"- this must be fixed using the \"gazelle:resolve\" directive",
targetListFromResults(filteredMatches), moduleName, mod.LineNumber, mod.Filepath)
"%[1]q, line %[2]d: multiple targets (%[3]s) may be imported with %[4]q: possible solutions:\n"+
"\t1. Disambiguate the above multiple targets by removing duplicate srcs entries.\n"+
"\t2. Use the '# gazelle:resolve py %[4]s TARGET_LABEL' BUILD file directive to resolve to one of the above targets.\n",
mod.Filepath, mod.LineNumber, targetListFromResults(filteredMatches), moduleName)
errs = append(errs, err)
continue POSSIBLE_MODULE_LOOP
}
Expand All @@ -263,7 +264,7 @@ func (py *Resolver) Resolve(
for _, err := range errs {
joinedErrs = fmt.Sprintf("%s%s\n", joinedErrs, err)
}
log.Printf("ERROR: failed to validate dependencies for target %q: %v\n", from.String(), joinedErrs)
log.Printf("ERROR: failed to validate dependencies for target %q:\n\n%v", from.String(), joinedErrs)
hasFatalError = true
}
}
Expand Down
4 changes: 3 additions & 1 deletion gazelle/python/testdata/invalid_imported_module/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import foo.bar

try:
import grpc

grpc_available = True
except ImportError:
grpc_available = False

_ = grpc
_ = bar(grpc)
11 changes: 11 additions & 0 deletions gazelle/python/testdata/invalid_imported_module/foo/BUILD.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
load("@rules_python//python:defs.bzl", "py_library")

py_library(
name = "bar_1",
srcs = ["bar.py"],
)

py_library(
name = "bar_2",
srcs = ["bar.py"],
)
Empty file.
17 changes: 15 additions & 2 deletions gazelle/python/testdata/invalid_imported_module/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,20 @@
expect:
exit_code: 1
stderr: |
gazelle: ERROR: failed to validate dependencies for target "//:invalid_imported_module": "grpc" at line 16 from "__init__.py" is an invalid dependency: possible solutions:
gazelle: ERROR: failed to validate dependencies for target "//:invalid_imported_module":
"__init__.py", line 15: multiple targets (//foo:bar_1, //foo:bar_2) may be imported with "foo.bar": possible solutions:
1. Disambiguate the above multiple targets by removing duplicate srcs entries.
2. Use the '# gazelle:resolve py foo.bar TARGET_LABEL' BUILD file directive to resolve to one of the above targets.
"__init__.py", line 15: "foo" is an invalid dependency: possible solutions:
1. Add it as a dependency in the requirements.txt file.
2. Use the '# gazelle:resolve py foo TARGET_LABEL' BUILD file directive to resolve to a known dependency.
3. Ignore it with a comment '# gazelle:ignore foo' in the Python file.
gazelle: ERROR: failed to validate dependencies for target "//:invalid_imported_module":
"__init__.py", line 18: "grpc" is an invalid dependency: possible solutions:
1. Add it as a dependency in the requirements.txt file.
2. Instruct Gazelle to resolve to a known dependency using the gazelle:resolve directive.
2. Use the '# gazelle:resolve py grpc TARGET_LABEL' BUILD file directive to resolve to a known dependency.
3. Ignore it with a comment '# gazelle:ignore grpc' in the Python file.

0 comments on commit 5b7e8cf

Please sign in to comment.