Skip to content

Don’t cause any file system file effects when trying to find an implicit workspace for a file #1350

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

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented May 24, 2024

When looking for a workspace that can handle a file, we were creating full-fledged workspaces along the way, which we would then discard if they couldn’t handle the file being opened. This had multiple problems:

  1. When background indexing is enabled, it caused semantic indexing of the workspace, which wrote files to a .index-build directory and was a waste of work
  2. When background indexing is enabled, it caused package resolution, which also created a .index-build folder to be created
  3. It caused a syntactic test index of the workspace, which was a waste of work.

To fix this, do multiple things:

  1. When creating a workspace, add a check right after build system creation. This allows us to early exit if the build system can’t handle the file and prevents us from generating the Workspace, fixing (1) and (3)
  2. Don’t call reloadPackage when creating a SwiftPMWorkspace. Instead, explicitly call generateBuildGraph once we committed to creating the workspace.

rdar://128682978

@ahoppen ahoppen requested review from bnbarham and hamishknight May 24, 2024 22:31
@ahoppen ahoppen requested a review from benlangmuir as a code owner May 24, 2024 22:31
@ahoppen
Copy link
Member Author

ahoppen commented May 25, 2024

@swift-ci Please test

@ahoppen ahoppen force-pushed the no-side-effects-when-searching-for-workspace branch from d31440c to e66a30a Compare May 25, 2024 21:32
@ahoppen
Copy link
Member Author

ahoppen commented May 25, 2024

@swift-ci Please test

@ahoppen
Copy link
Member Author

ahoppen commented May 25, 2024

@swift-ci Please test Windows

1 similar comment
@ahoppen
Copy link
Member Author

ahoppen commented May 27, 2024

@swift-ci Please test Windows

Comment on lines 64 to 77
let buildSystem: BuildSystem
if let defaultBuildSystem {
buildSystem = defaultBuildSystem
} else if let buildServer = await createBuildServerBuildSystem(rootPath: rootPath) {
buildSystem = buildServer
} else if let swiftpm = await createSwiftPMBuildSystem(rootUrl: rootUrl) {
buildSystem = swiftpm
} else if let compdb = createCompilationDatabaseBuildSystem(rootPath: rootPath) {
buildSystem = compdb
} else {
logger.error("Could not set up a build system at '\(rootUri.forLogging)'")
return nil
}
return buildSystem
Copy link
Contributor

Choose a reason for hiding this comment

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

Could turn these into early returns now

Comment on lines 606 to 607
let projectRoot = await buildSystem.projectRoot
return await buildSystem.fileHandlingCapability(for: uri) == .handled && !projectRoots.contains(projectRoot)
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we reject !projectRoots.contains(projectRoot) cases sooner, even before we attempt to create the workspace? Could the build system root ever differ from the workspace root?

Copy link
Member Author

@ahoppen ahoppen May 28, 2024

Choose a reason for hiding this comment

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

We do reject !projectRoots.contains(projectRoot) before we create the workspace because it’s part of the satisfying closure passed to createWorkspace.

We cannot perform this check before generating the build graph because we projectRoot is a property of buildSystem. And when you create a SwiftPMBuildSystem, for the Sources folder, I think it creates a build system with project root set to the folder that contains the Package.swift.

But we can hoist this check to be before the build graph generation, which makes sense.

Copy link
Contributor

@hamishknight hamishknight May 28, 2024

Choose a reason for hiding this comment

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

Just to clarify, I meant rejecting before the call to createWorkspace, based on the workspace URL. But yeah looks like the build system root can differ from the workspace. Doing it before the build graph generation makes sense 👍

…cit workspace for a file

When looking for a workspace that can handle a file, we were creating full-fledged workspaces along the way, which we would then discard if they couldn’t handle the file being opened. This had multiple problems:
1. When background indexing is enabled, it caused semantic indexing of the workspace, which wrote files to a `.index-build` directory and was a waste of work
2. When background indexing is enabled, it caused package resolution, which also created a `.index-build` folder to be created
3. It caused a syntactic test index of the workspace, which was a waste of work.

To fix this, do multiple things:
1. When creating a workspace, add a check right after build system creation. This allows us to early exit if the build system can’t handle the file and prevents us from generating the `Workspace`, fixing (1) and (3)
2. Don’t call `reloadPackage` when creating a `SwiftPMWorkspace`. Instead, explicitly call `generateBuildGraph` once we committed to creating the workspace.
@ahoppen ahoppen force-pushed the no-side-effects-when-searching-for-workspace branch from e66a30a to f85d821 Compare May 28, 2024 15:30
@ahoppen
Copy link
Member Author

ahoppen commented May 28, 2024

@swift-ci Please test

@ahoppen
Copy link
Member Author

ahoppen commented May 28, 2024

@swift-ci Please test Windows

1 similar comment
@ahoppen
Copy link
Member Author

ahoppen commented May 28, 2024

@swift-ci Please test Windows

@ahoppen ahoppen merged commit 8765bb1 into swiftlang:main May 28, 2024
3 checks passed
@ahoppen ahoppen deleted the no-side-effects-when-searching-for-workspace branch May 28, 2024 18:27
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.

2 participants