Skip to content

Commit fda8ec4

Browse files
committed
CARGO: drop org.rust.cargo.fetch.out.dir feature
The feature was initial attempt to support `include` macro with `OUT_DIR` variable. It has two major issues: - invocation of Cargo with `--build-plan` option force Cargo to recompile everything from the scratch next time - the option is supposed to be [removed](rust-lang/cargo#7614). Also, there is more powerful feature - `org.rust.cargo.evaluate.build.scripts` that allows plugin to know about all generated `cfg` options and environment variables (including `OUT_DIR`). So I think we can drop `--build-plan` support since it has major cons and can be superseded by `org.rust.cargo.evaluate.build.scripts` feature
1 parent 49273d9 commit fda8ec4

File tree

6 files changed

+126
-280
lines changed

6 files changed

+126
-280
lines changed

src/main/kotlin/org/rust/cargo/toolchain/impl/CargoMetadata.kt

+3-31
Original file line numberDiff line numberDiff line change
@@ -303,12 +303,10 @@ object CargoMetadata {
303303

304304
fun clean(
305305
project: Project,
306-
buildMessages: BuildMessages? = null,
307-
buildPlan: CargoBuildPlan? = null
306+
buildMessages: BuildMessages? = null
308307
): CargoWorkspaceData {
309308
val fs = LocalFileSystem.getInstance()
310309
val members = project.workspace_members
311-
val variables = PackageVariables.from(buildPlan)
312310
val packageIdToNode = project.resolve.nodes.associateBy { it.id }
313311
return CargoWorkspaceData(
314312
project.packages.map { pkg ->
@@ -319,7 +317,7 @@ object CargoMetadata {
319317
}
320318
val enabledFeatures = resolveNode?.features.orEmpty().toSet() // features enabled by Cargo
321319
val pkgBuildMessages = buildMessages?.get(pkg.id).orEmpty()
322-
pkg.clean(fs, pkg.id in members, variables, enabledFeatures, pkgBuildMessages)
320+
pkg.clean(fs, pkg.id in members, enabledFeatures, pkgBuildMessages)
323321
},
324322
project.resolve.nodes.associate { node ->
325323
val dependencySet = if (node.deps != null) {
@@ -341,7 +339,6 @@ object CargoMetadata {
341339
private fun Package.clean(
342340
fs: LocalFileSystem,
343341
isWorkspaceMember: Boolean,
344-
variables: PackageVariables,
345342
enabledFeatures: Set<String>,
346343
buildMessages: List<CompilerMessage>
347344
): CargoWorkspaceData.Package {
@@ -389,8 +386,7 @@ object CargoMetadata {
389386
"CARGO_CRATE_NAME" to name.replace('-', '_'),
390387
)
391388

392-
val outDirPath = buildScriptMessage?.out_dir ?: variables.getOutDirPath(this)
393-
val outDir = outDirPath
389+
val outDir = buildScriptMessage?.out_dir
394390
?.let { root.fileSystem.refreshAndFindFileByPath(it) }
395391
?.let { if (isWorkspaceMember) it else it.canonicalFile }
396392

@@ -534,27 +530,3 @@ object CargoMetadata {
534530
private fun Target.replacePaths(replacer: (String) -> String): Target =
535531
copy(src_path = replacer(src_path))
536532
}
537-
538-
private class PackageVariables(private val variables: Map<PackageInfo, Map<String, String>>) {
539-
540-
fun getOutDirPath(pkg: CargoMetadata.Package): String? = getValue(pkg, "OUT_DIR")
541-
542-
fun getValue(pkg: CargoMetadata.Package, variableName: String): String? {
543-
val key = PackageInfo(pkg.name, pkg.version)
544-
return variables[key]?.get(variableName)
545-
}
546-
547-
companion object {
548-
fun from(buildPlan: CargoBuildPlan?): PackageVariables {
549-
val result = mutableMapOf<PackageInfo, Map<String, String>>()
550-
for ((packageName, packageVersion, compileMode, variables) in buildPlan?.invocations.orEmpty()) {
551-
if (compileMode != "run-custom-build") continue
552-
val targetInfo = PackageInfo(packageName, packageVersion)
553-
result[targetInfo] = variables
554-
}
555-
return PackageVariables(result)
556-
}
557-
}
558-
559-
private data class PackageInfo(val name: String, val version: String)
560-
}

src/main/kotlin/org/rust/cargo/toolchain/tools/Cargo.kt

+1-33
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,10 @@ open class Cargo(toolchain: RsToolchain, useWrapper: Boolean = false)
129129
): CargoWorkspaceData {
130130
val rawData = fetchMetadata(owner, projectDirectory, listener)
131131
val buildScriptsInfo = fetchBuildScriptsInfo(owner, projectDirectory, listener)
132-
val buildPlan = if (buildScriptsInfo?.containsOutDirInfo != true) {
133-
fetchBuildPlan(owner, projectDirectory, listener)
134-
} else {
135-
null
136-
}
137132

138133
val (rawDataAdjusted, buildScriptsInfoAdjusted) =
139134
replacePathsSymlinkIfNeeded(rawData, buildScriptsInfo, projectDirectory)
140-
return CargoMetadata.clean(rawDataAdjusted, buildScriptsInfoAdjusted, buildPlan)
135+
return CargoMetadata.clean(rawDataAdjusted, buildScriptsInfoAdjusted)
141136
}
142137

143138
@Throws(ExecutionException::class)
@@ -216,29 +211,6 @@ open class Cargo(toolchain: RsToolchain, useWrapper: Boolean = false)
216211
return BuildMessages(messages)
217212
}
218213

219-
private fun fetchBuildPlan(
220-
owner: Project,
221-
projectDirectory: Path,
222-
listener: ProcessListener?
223-
): CargoBuildPlan? {
224-
if (!isFeatureEnabled(RsExperiments.FETCH_OUT_DIR)) return null
225-
Testmarks.fetchBuildPlan.hit()
226-
val additionalArgs = mutableListOf("-Z", "unstable-options", "--all-targets", "--build-plan")
227-
// Hack to make cargo think that unstable options are available because we need unstable `--build-plan` option here
228-
val envs = EnvironmentVariablesData.create(mapOf(
229-
RUSTC_BOOTSTRAP to "1"
230-
), true)
231-
return try {
232-
val json = CargoCommandLine("build", projectDirectory, additionalArgs, environmentVariables = envs)
233-
.execute(owner, listener = listener)
234-
.stdout
235-
Gson().fromJson(json, CargoBuildPlan::class.java)
236-
} catch (e: Exception) {
237-
LOG.warn("Failed to fetch build-plan", e)
238-
null
239-
}
240-
}
241-
242214
/**
243215
* If project directory is 'source' which is a symlink to 'target' directory,
244216
* then `cargo metadata` command inside 'source' directory produces output with 'target' paths.
@@ -587,10 +559,6 @@ open class Cargo(toolchain: RsToolchain, useWrapper: Boolean = false)
587559
return needInstall
588560
}
589561
}
590-
591-
object Testmarks {
592-
val fetchBuildPlan = Testmark("fetchBuildPlan")
593-
}
594562
}
595563

596564
// Note: the class is used as a cache key, so it must be immutable and must have a correct equals/hashCode

src/main/kotlin/org/rust/ide/experiments/RsExperiments.kt

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package org.rust.ide.experiments
88
object RsExperiments {
99
const val BUILD_TOOL_WINDOW = "org.rust.cargo.build.tool.window"
1010
const val TEST_TOOL_WINDOW = "org.rust.cargo.test.tool.window"
11-
const val FETCH_OUT_DIR = "org.rust.cargo.fetch.out.dir"
1211
const val EVALUATE_BUILD_SCRIPTS = "org.rust.cargo.evaluate.build.scripts"
1312
const val CARGO_FEATURES_SETTINGS_GUTTER = "org.rust.cargo.features.settings.gutter"
1413
const val MACROS_NEW_ENGINE = "org.rust.macros.new.engine"

src/main/resources-nightly/META-INF/experiments.xml

-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
<experimentalFeature id="org.rust.cargo.test.tool.window" percentOfUsers="100">
77
<description>Show test results in Test Tool Window</description>
88
</experimentalFeature>
9-
<experimentalFeature id="org.rust.cargo.fetch.out.dir" percentOfUsers="0">
10-
<description>Fetch `OUT_DIR` environment variable value. It's needed to make proper name resolution for code included via `include` macro</description>
11-
</experimentalFeature>
129
<experimentalFeature id="org.rust.cargo.evaluate.build.scripts" percentOfUsers="0">
1310
<description>Run build scripts while project refresh. It allows collecting information about generated items like `cfg` options, environment variables, etc</description>
1411
</experimentalFeature>

src/main/resources-stable/META-INF/experiments.xml

-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
<experimentalFeature id="org.rust.cargo.test.tool.window" percentOfUsers="100">
77
<description>Show test results in Test Tool Window</description>
88
</experimentalFeature>
9-
<experimentalFeature id="org.rust.cargo.fetch.out.dir" percentOfUsers="0">
10-
<description>Fetch `OUT_DIR` environment variable value. It's needed to make proper name resolution for code included via `include` macro</description>
11-
</experimentalFeature>
129
<experimentalFeature id="org.rust.cargo.evaluate.build.scripts" percentOfUsers="0">
1310
<description>Run build scripts while project refresh. It allows collecting information about generated items like `cfg` options, environment variables, etc</description>
1411
</experimentalFeature>

0 commit comments

Comments
 (0)