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

Verify that only targets that are manifest dependencies can be imported. #3562

Merged
merged 6 commits into from Jul 6, 2022

Conversation

artemcm
Copy link
Contributor

@artemcm artemcm commented Jun 16, 2021

This change introduces a build verification step that attempts to detect scenarios where a target contains an import of another target in the package without declaring the imported target as a dependency in the manifest.
This is done via SwiftDriver's import-prescan capability which relies on libSwiftScan to quickly parse a target's sources and identify all imported modules.

@artemcm artemcm force-pushed the VerifyOnlyDependencyTargetsImported branch from b9faf24 to 6af65b0 Compare June 16, 2021 23:16
@artemcm artemcm changed the title [WIP][DNM] Verify that only targets that are manifest dependencies can be imported. Verify that only targets that are manifest dependencies can be imported. Jun 23, 2021
@artemcm artemcm force-pushed the VerifyOnlyDependencyTargetsImported branch 3 times, most recently from 3eda4d7 to 3b840ca Compare June 25, 2021 18:04
@artemcm
Copy link
Contributor Author

artemcm commented Jun 25, 2021

@swift-ci please smoke test

@artemcm artemcm marked this pull request as ready for review June 25, 2021 18:11
@tomerd tomerd requested a review from elsh June 25, 2021 19:43
@tomerd
Copy link
Member

tomerd commented Jun 25, 2021

cc @elsh

Copy link
Member

@neonichu neonichu left a comment

Choose a reason for hiding this comment

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

Thanks a lot for this PR, Artem!

@artemcm artemcm force-pushed the VerifyOnlyDependencyTargetsImported branch 2 times, most recently from 8e377b8 to 11e0f93 Compare June 25, 2021 20:49
@artemcm
Copy link
Contributor Author

artemcm commented Jun 25, 2021

@swift-ci please smoke test

@artemcm
Copy link
Contributor Author

artemcm commented Jun 25, 2021

Oof, emitCommandLine has side-effects which change build outputs. Its usage in this PR caused several tests to fail because we're now building more than we used to.

I'm looking at how to work around this/remove these side-effects.

@artemcm artemcm force-pushed the VerifyOnlyDependencyTargetsImported branch from 11e0f93 to c8ba669 Compare June 25, 2021 22:10
@artemcm
Copy link
Contributor Author

artemcm commented Jun 25, 2021

@swift-ci please smoke test

@artemcm
Copy link
Contributor Author

artemcm commented Jun 25, 2021

The current failure seems to be due to the fact that the toolchain being tested does not come with libSwiftScan, so the driver falls back on invoking the scanning action using a swift-frontend invocation, which works just as well, but causes the driver to emit an additional warning:

warning: Fallback to `swift-frontend` dependency scanner invocation

Which is not expected to be seen in some of these tests.

@artemcm
Copy link
Contributor Author

artemcm commented Jun 25, 2021

Together with apple/swift-driver#731
@swift-ci please smoke test

@artemcm
Copy link
Contributor Author

artemcm commented Jun 28, 2021

@swift-ci please smoke test

2 similar comments
@artemcm
Copy link
Contributor Author

artemcm commented Jun 28, 2021

@swift-ci please smoke test

@artemcm
Copy link
Contributor Author

artemcm commented Jun 29, 2021

@swift-ci please smoke test

@tomerd
Copy link
Member

tomerd commented Jun 29, 2021

@swift-ci please test package compatibility

@tomerd
Copy link
Member

tomerd commented Jun 29, 2021

@artemcm also running the compatibility test suite to make sure we dont break any packages

@neonichu
Copy link
Member

neonichu commented Jun 29, 2021

I don't see how the Linux self-hosted failure could be caused by the changes in this PR and I also can't reproduce them locally so far. I think we should still understand why they're failing even though we don't require them to pass for merging.

@artemcm
Copy link
Contributor Author

artemcm commented Jun 30, 2021

@swift-ci please smoke test

@abertelrud abertelrud added the WIP Work in progress label Sep 8, 2021
@ktoso
Copy link
Member

ktoso commented Oct 6, 2021

Coming over here with some friendly cheer leading -- hit such issue hard right now and took us a while to debug it.
Really really looking forward to this PR landing, would have saved us much time ❤️

@artemcm artemcm force-pushed the VerifyOnlyDependencyTargetsImported branch from 54097aa to 2e620d1 Compare October 26, 2021 23:11
@artemcm
Copy link
Contributor Author

artemcm commented Oct 26, 2021

@swift-ci please smoke test

@ktoso
Copy link
Member

ktoso commented Oct 27, 2021

Thanks @artemcm !

I talked with @tomerd and @abertelrud and we'll try and see if we could solve the swift-syntax being usable here, and then we could try to use that for the parsing instead... Thank you for the rebase, we'll report back soon what the plan here is!

@artemcm
Copy link
Contributor Author

artemcm commented Oct 27, 2021

In the meantime, I've reversed the build command-line option I had here from a disable to an enable option, if we would like to land this as an opt-in feature for the time being. It would at the very least be useful to have enabled in CI configurations.

@artemcm artemcm force-pushed the VerifyOnlyDependencyTargetsImported branch from 2e620d1 to 60bac7b Compare October 27, 2021 17:05
@artemcm
Copy link
Contributor Author

artemcm commented Oct 27, 2021

@swift-ci please smoke test

@artemcm
Copy link
Contributor Author

artemcm commented Oct 27, 2021

@swift-ci please smoke test Linux platform

1 similar comment
@artemcm
Copy link
Contributor Author

artemcm commented Oct 27, 2021

@swift-ci please smoke test Linux platform

@ktoso
Copy link
Member

ktoso commented Jul 5, 2022

Periodic ping here... this has hit us again and broke users trying to play around with distributed actors because it sneaked through CI which did not validate it, nor did it show up on dev machines, since we're not using Xcode most of the time; apple/swift-distributed-actors#981

@SDGGiesbrecht
Copy link
Contributor

+1

Originally this did not seem particularly important. But ever since Xcode started adding import statements behind your back, stray invalid imports have been spreading like wildfire. Consequently, this feature has rocketed to the very highest ranks of my most wanted list.

This change introduces a build verification step that attempts to detect scenarios where a target contains an `import` of another target in the package without declaring the imported target as a dependency in the manifest.
This is done via SwiftDriver's import-prescan capability which relies on libSwiftScan to quickly parse a target's sources and identify all `import`ed modules.

Related to rdar://79423257
…ted source-code may not be available at the time of scan.
`--disable-explicit-target-dependency-import-checking` prevents the verification code from being run as a just-in-case option to opt-out from this warning
…n to an opt-in `enable` option.

While we work on optimizing incremental build performance of this check, it would be nice to have it as an opt-in option.
@artemcm artemcm force-pushed the VerifyOnlyDependencyTargetsImported branch from 60bac7b to a4114eb Compare July 6, 2022 17:50
…warning', or 'none' modes, with 'none' being the default.
@artemcm
Copy link
Contributor Author

artemcm commented Jul 6, 2022

@swift-ci smoke test

@neonichu neonichu merged commit 2af09a8 into apple:main Jul 6, 2022
@ktoso
Copy link
Member

ktoso commented Jul 7, 2022

Thank you very much folks!

@@ -0,0 +1 @@
print(baz(x: 11))
Copy link
Member

Choose a reason for hiding this comment

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

@artemcm @neonichu this test started failing recently, due to

error: cannot find 'baz' in scope
print(baz(x: 11))

but only on macOS. I'm not sure how it was suposed to work in the first place and how it worked before and still works on other platforms, since baz(x:) is not defined anywhere?

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

I think the error is expected, right? The test is using XCTAssertThrowsError in all cases.

Copy link
Member

Choose a reason for hiding this comment

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

I think the problem is that Target A imports another target (B) in the package without declaring it a dependency. isn't present in the output at all.

Copy link
Member

Choose a reason for hiding this comment

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

My assumption would be that <unknown>:0: error: unknown argument: '-dwarf-version=4 interferes with the import detection, e.g. we may not get a list of imports at all and from that follows the test failure.

AFAIK, we haven't updated the installed Swift version, so it's a bit of a mystery how this broke?

Copy link
Member

Choose a reason for hiding this comment

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

Actually maybe this will be the fix? apple/swift-driver#1502

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.

None yet

8 participants