Skip to content

Improve error message about the PackageGraphError.productDependencyNotFound #7419

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

Merged
merged 5 commits into from
Mar 30, 2024

Conversation

k-kohey
Copy link
Contributor

@k-kohey k-kohey commented Mar 24, 2024

Fixed a bug in the productDependencyNotFound error message

Motivation:

fix #7398

The above issue suggests the following two defects.

  1. package dependency resolution changes depending on the order of packages (alphabetical order)
  2. the phrase "Did you meen..." in the error message in the error message is not on target.

Modifications:

The first problem is as described in the issue, if users rename the directory containing Package.swift from repro to zzz, the "Did you meen..." will appear. Essentially, the error minutes should be displayed in full, regardless of the name of the directory. This is due to the fact that the loop used to resolve dependencies depends on the alphabetical order of the directories and the side effect on allTargetName inside the loop. Therefore, the side effect for allTargetName inside the loop has been moved to the outside of the loop.

The second problem is that when dependency A of a package is not found, a strange suggestion "Did you meen A? ", which is a strange suggestion. This is because when a dependency is not found, the message "Did you meen <target name>" is printed if there is a dependency with a similar name (even the exact same name). Thus, if the names are the same, "Did you meen .product(name: ... , package: "swift-argugument")"" instead of the found target name.

Result:

Command to execute:
swift build

Result of command:.
error: 'repro': product 'ArgumentParser' required by package 'repro' target 'repro' not found. Did you mean '.product(name: "ArgumentParser", package: "swift-argument-parser")'?

Condition:
The following steps were taken to create the environment.

  1. mkdir repro
  2. cd repro
  3. swift package init --type executable
  4. Open Package.swift and make sure it has this content:
// swift-tools-version: 5.10

import PackageDescription

let package = Package(
    name: "repro",
    dependencies: [
        .package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.3.0")
    ],
    targets: [
        .executableTarget(
            name: "repro",
            dependencies: ["ArgumentParser"]
        )
    ]
)


testDiagnostics(observability.diagnostics) { result in
result.check(
diagnostic: "product 'zzy' required by package 'aaa' target 'aaa' not found. Did you mean 'zzz'?",
Copy link
Contributor Author

@k-kohey k-kohey Mar 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR fix will cause the error message to include "Did you mean...".
Previously, this message was not displayed depending on the name of the package directory.

@k-kohey k-kohey changed the title [wip] make allTargetNames Type Array<String> Fixed a bug in the productDependencyNotFound error message Mar 25, 2024
@k-kohey k-kohey changed the title Fixed a bug in the productDependencyNotFound error message Fixed a bug in the PackageGraphError.productDependencyNotFound error message Mar 25, 2024
if dependencyProductInDecl {
return "product '\(dependencyProductName)' is declared in the same package '\(package)' and can't be used as a dependency for target '\(targetName)'."
} else {
var description = "product '\(dependencyProductName)' required by package '\(package)' target '\(targetName)' \(dependencyPackageName.map{ "not found in package '\($0)'" } ?? "not found")."
if let similarProductName {
if let similarProductName, let packageContainingSimilarProduct {
description += " Did you mean '.product(name: \"\(similarProductName)\", package: \"\(packageContainingSimilarProduct)\")'?"
Copy link
Contributor Author

@k-kohey k-kohey Mar 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote the following test code to test the output of this message.

func test_() throws {
    let fs = InMemoryFileSystem(emptyFiles:
        "/zzz/Sources/zzz/main.swift",
        "/aaa/Sources/aaa/source.swift"
    )

    let observability = ObservabilitySystem.makeForTesting()
    let g = try loadModulesGraph(
        fileSystem: fs,
        manifests: [
            Manifest.createRootManifest(
                displayName: "zzz",
                path: "/zzz",
                dependencies: [
                    .remoteSourceControl(
                        url: "https://example.com/example.git",
                        requirement: .exact("1.3.0")
                    )
                ],
                products: [],
                targets: [
                    TargetDescription(
                        name: "zzz",
                        dependencies: ["aaa"],
                        type: .executable
                    )
                ]),
            Manifest.createRemoteSourceControlManifest(
                displayName: "aaa",
                url: "https://example.com/example.git",
                path: "/aaa",
                version: "1.3.0",
                products: [
                    ProductDescription(
                        name: "aaa",
                        type: .library(.automatic),
                        targets: ["aaa"]
                    )
                ],
                targets: [
                    TargetDescription(name: "aaa")
                ]
            )
        ],
        observabilityScope: observability.topScope
    )

    testDiagnostics(observability.diagnostics) { result in
        result.check(
            diagnostic: "error: 'zzz': product 'aaa' required by package 'zzz' target 'repro' not found. Did you mean '.product(name: \"aaa\", package: \"aaa\")'?",
            severity: .error
        )
    }
}

However, this test fails with the message "test_(): failed - No diagnostics left to check".
I would like to know how to solve this problem.

@k-kohey k-kohey marked this pull request as ready for review March 25, 2024 13:29
@k-kohey k-kohey changed the title Fixed a bug in the PackageGraphError.productDependencyNotFound error message Improve error message about the PackageGraphError.productDependencyNotFound Mar 26, 2024
@kateinoigakukun
Copy link
Member

@swift-ci test

@kateinoigakukun
Copy link
Member

Thank you for your contribution!

@kateinoigakukun kateinoigakukun merged commit ea1730b into swiftlang:main Mar 30, 2024
5 checks passed
furby-tm pushed a commit to wabiverse/swift-package-manager that referenced this pull request May 15, 2024
…tFound (swiftlang#7419)

Fixed a bug in the productDependencyNotFound error message

### Motivation:

fix swiftlang#7398

The above issue suggests the following two defects.

1. package dependency resolution changes depending on the order of
packages (alphabetical order)
2. the phrase "Did you meen..." in the error message in the error
message is not on target.


### Modifications:

The first problem is as described in the issue, if users rename the
directory containing Package.swift from repro to zzz, the "Did you
meen..." will appear. will appear. Essentially, the error minutes should
be displayed in full, regardless of the name of the directory. This is
due to the fact that the loop used to resolve dependencies depends on
the alphabetical order of the directories and the side effect on
allTargetName inside the loop. Therefore, the side effect for
allTargetName inside the loop has been moved to the outside of the loop.

The second problem is that when dependency A of a package is not found,
a strange suggestion "Did you meen `A`? ", which is a strange
suggestion. This is because when a dependency is not found, the message
"Did you meen `<target name>`" is printed if there is a dependency with
a similar name (even the exact same name). Thus, if the names are the
same, "Did you meen `.product(name: ... , package:
"swift-argugument")"`" instead of the found target name.

### Result:

Command to execute: 
`swift build`

Result of command:.
`error: 'repro': product 'ArgumentParser' required by package 'repro'
target 'repro' not found. Did you mean '.product(name: "ArgumentParser",
package: "swift-argument-parser")'?`

Condition:
The following steps were taken to create the environment.

1. mkdir repro
1. cd repro
1. swift package init --type executable
1. Open Package.swift and make sure it has this content:

```swift
// swift-tools-version: 5.10

import PackageDescription

let package = Package(
    name: "repro",
    dependencies: [
        .package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.3.0")
    ],
    targets: [
        .executableTarget(
            name: "repro",
            dependencies: ["ArgumentParser"]
        )
    ]
)
```

---------

Co-authored-by: Yuta Saito <[email protected]>
furby-tm pushed a commit to wabiverse/swift-package-manager that referenced this pull request May 15, 2024
…tFound (swiftlang#7419)

Fixed a bug in the productDependencyNotFound error message

### Motivation:

fix swiftlang#7398

The above issue suggests the following two defects.

1. package dependency resolution changes depending on the order of
packages (alphabetical order)
2. the phrase "Did you meen..." in the error message in the error
message is not on target.


### Modifications:

The first problem is as described in the issue, if users rename the
directory containing Package.swift from repro to zzz, the "Did you
meen..." will appear. will appear. Essentially, the error minutes should
be displayed in full, regardless of the name of the directory. This is
due to the fact that the loop used to resolve dependencies depends on
the alphabetical order of the directories and the side effect on
allTargetName inside the loop. Therefore, the side effect for
allTargetName inside the loop has been moved to the outside of the loop.

The second problem is that when dependency A of a package is not found,
a strange suggestion "Did you meen `A`? ", which is a strange
suggestion. This is because when a dependency is not found, the message
"Did you meen `<target name>`" is printed if there is a dependency with
a similar name (even the exact same name). Thus, if the names are the
same, "Did you meen `.product(name: ... , package:
"swift-argugument")"`" instead of the found target name.

### Result:

Command to execute: 
`swift build`

Result of command:.
`error: 'repro': product 'ArgumentParser' required by package 'repro'
target 'repro' not found. Did you mean '.product(name: "ArgumentParser",
package: "swift-argument-parser")'?`

Condition:
The following steps were taken to create the environment.

1. mkdir repro
1. cd repro
1. swift package init --type executable
1. Open Package.swift and make sure it has this content:

```swift
// swift-tools-version: 5.10

import PackageDescription

let package = Package(
    name: "repro",
    dependencies: [
        .package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.3.0")
    ],
    targets: [
        .executableTarget(
            name: "repro",
            dependencies: ["ArgumentParser"]
        )
    ]
)
```

---------

Co-authored-by: Yuta Saito <[email protected]>
bnbarham pushed a commit to bnbarham/swift-package-manager that referenced this pull request May 18, 2024
…tFound (swiftlang#7419)

Fixed a bug in the productDependencyNotFound error message

### Motivation:

fix swiftlang#7398

The above issue suggests the following two defects.

1. package dependency resolution changes depending on the order of
packages (alphabetical order)
2. the phrase "Did you meen..." in the error message in the error
message is not on target.

### Modifications:

The first problem is as described in the issue, if users rename the
directory containing Package.swift from repro to zzz, the "Did you
meen..." will appear. will appear. Essentially, the error minutes should
be displayed in full, regardless of the name of the directory. This is
due to the fact that the loop used to resolve dependencies depends on
the alphabetical order of the directories and the side effect on
allTargetName inside the loop. Therefore, the side effect for
allTargetName inside the loop has been moved to the outside of the loop.

The second problem is that when dependency A of a package is not found,
a strange suggestion "Did you meen `A`? ", which is a strange
suggestion. This is because when a dependency is not found, the message
"Did you meen `<target name>`" is printed if there is a dependency with
a similar name (even the exact same name). Thus, if the names are the
same, "Did you meen `.product(name: ... , package:
"swift-argugument")"`" instead of the found target name.

### Result:

Command to execute:
`swift build`

Result of command:.
`error: 'repro': product 'ArgumentParser' required by package 'repro'
target 'repro' not found. Did you mean '.product(name: "ArgumentParser",
package: "swift-argument-parser")'?`

Condition:
The following steps were taken to create the environment.

1. mkdir repro
1. cd repro
1. swift package init --type executable
1. Open Package.swift and make sure it has this content:

```swift
// swift-tools-version: 5.10

import PackageDescription

let package = Package(
    name: "repro",
    dependencies: [
        .package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.3.0")
    ],
    targets: [
        .executableTarget(
            name: "repro",
            dependencies: ["ArgumentParser"]
        )
    ]
)
```

---------

Co-authored-by: Yuta Saito <[email protected]>
(cherry picked from commit ea1730b)
shahmishal pushed a commit that referenced this pull request May 18, 2024
*Explanation*: I went through the last few months of PRs to make sure
anything relevant is cherry-picked. Most of these are NFC but
cherry-picking will help with conflicts. The main are:
* Better error message -
#7419
* Fix for visionOS for `--build-system xcode` -
#7448
* Package registry fix -
#7454
* Manifest editing API for adding target dependencies -
#7552
* Various sendable annotations

*Scope*: Package manifests/graphs with duplicate product/target names.
*Risk*: Very low
*Reviewed By*: Various, mostly @MaxDesiatov

---------

Co-authored-by: Max Desiatov <[email protected]>
Co-authored-by: k-kohey <[email protected]>
Co-authored-by: miharu <[email protected]>
Co-authored-by: Ryu <[email protected]>
Co-authored-by: Philipp Wallrich <[email protected]>
Co-authored-by: Boris Bügling <[email protected]>
Co-authored-by: coffmark <[email protected]>
Co-authored-by: Doug Gregor <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unclear missing product error depends on ordering of package names
2 participants