Skip to content

Commit 981fd5d

Browse files
committed
Inherit prototools version.
1 parent b147586 commit 981fd5d

10 files changed

Lines changed: 282 additions & 17 deletions

File tree

.yarn/versions/94da93c8.yml

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
releases:
2-
"@moonrepo/cli": patch
3-
"@moonrepo/core-linux-arm64-gnu": patch
4-
"@moonrepo/core-linux-arm64-musl": patch
5-
"@moonrepo/core-linux-x64-gnu": patch
6-
"@moonrepo/core-linux-x64-musl": patch
7-
"@moonrepo/core-macos-arm64": patch
8-
"@moonrepo/core-macos-x64": patch
9-
"@moonrepo/core-windows-x64-msvc": patch
2+
'@moonrepo/cli': patch
3+
'@moonrepo/core-linux-arm64-gnu': patch
4+
'@moonrepo/core-linux-arm64-musl': patch
5+
'@moonrepo/core-linux-x64-gnu': patch
6+
'@moonrepo/core-linux-x64-musl': patch
7+
'@moonrepo/core-macos-arm64': patch
8+
'@moonrepo/core-macos-x64': patch
9+
'@moonrepo/core-windows-x64-msvc': patch
10+
'@moonrepo/types': patch
11+
12+
declined:
13+
- '@moonrepo/report'
14+
- '@moonrepo/runtime'
15+
- website

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44

55
#### 🚀 Updates
66

7+
- Added a new setting for toolchain plugins, `versionFromPrototools`, which controls how we inherit
8+
a version from the root `.prototools` file. By default this is enabled, but can be configured with
9+
a string if the IDs don't match.
710
- Updated `moon setup` to also install toolchain plugins.
811

912
#### 🐞 Fixes
1013

14+
- Fixed an issue where toolchain plugins do not inherit versions from `.prototools`.
1115
- Fixed an issue where a file lock would be created for proto installation, even when it didn't need
1216
to be installed.
1317

crates/config/src/toolchain/plugin_config.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
1-
use crate::config_struct;
2-
use schematic::Config;
1+
use crate::{config_enum, config_struct};
2+
use schematic::{Config, Schematic};
33
use serde_json::Value;
44
use std::collections::BTreeMap;
55
use version_spec::UnresolvedVersionSpec;
66
use warpgate_api::PluginLocator;
77

8+
config_enum!(
9+
/// Strategy in which to inherit a version from `.prototools`.
10+
#[derive(Schematic)]
11+
#[serde(untagged)]
12+
pub enum ToolchainPluginVersionFrom {
13+
Enabled(bool),
14+
Id(String),
15+
}
16+
);
17+
18+
impl Default for ToolchainPluginVersionFrom {
19+
fn default() -> Self {
20+
Self::Enabled(true)
21+
}
22+
}
23+
824
config_struct!(
925
/// Configures an individual toolchain.
1026
#[derive(Config)]
@@ -20,6 +36,11 @@ config_struct!(
2036
/// The version of the toolchain to download and install.
2137
pub version: Option<UnresolvedVersionSpec>,
2238

39+
/// Inherit the version from the root `.prototools`.
40+
/// When true, matches using the same ID, otherwise a
41+
/// string can be provided for a custom ID.
42+
pub version_from_prototools: ToolchainPluginVersionFrom,
43+
2344
/// Arbitrary configuration that'll be passed to the WASM plugin.
2445
#[setting(flatten)]
2546
pub config: BTreeMap<String, Value>,

crates/config/src/toolchain_config.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,45 @@ impl ToolchainConfig {
242242
false
243243
}
244244

245+
pub fn inherit_proto_for_plugins(
246+
&mut self,
247+
proto_config: &proto_core::ProtoConfig,
248+
) -> miette::Result<()> {
249+
use moon_common::color;
250+
use tracing::trace;
251+
252+
for (id, config) in &mut self.plugins {
253+
if config.version.is_some() {
254+
continue;
255+
}
256+
257+
let proto_id = match &config.version_from_prototools {
258+
ToolchainPluginVersionFrom::Enabled(enabled) => {
259+
if *enabled {
260+
id.as_str().strip_prefix("unstable_").unwrap_or(id.as_str())
261+
} else {
262+
continue;
263+
}
264+
}
265+
ToolchainPluginVersionFrom::Id(custom_id) => custom_id,
266+
};
267+
268+
if let Some(version) = proto_config.versions.get(proto_id) {
269+
trace!(
270+
"Inheriting {} version {} from .prototools",
271+
color::id(id),
272+
version
273+
);
274+
275+
config.version = Some(version.req.to_owned());
276+
}
277+
}
278+
279+
Ok(())
280+
}
281+
245282
pub fn inherit_proto(&mut self, proto_config: &proto_core::ProtoConfig) -> miette::Result<()> {
283+
self.inherit_proto_for_plugins(proto_config)?;
246284
self.inherit_proto_bun(proto_config)?;
247285
self.inherit_proto_deno(proto_config)?;
248286
self.inherit_proto_node(proto_config)?;
@@ -285,7 +323,7 @@ impl ToolchainConfig {
285323
use tracing::warn;
286324

287325
warn!(
288-
"The legacy Rust toolchain and WASM based Rust toolchain must not be used together"
326+
"The legacy Rust toolchain and WASM based Rust toolchain must not be used together!"
289327
);
290328
}
291329

crates/config/tests/project_config_test.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -677,10 +677,9 @@ toolchain:
677677
assert_eq!(
678678
config.toolchain.plugins.get("example").unwrap(),
679679
&ProjectToolchainEntry::Config(ToolchainPluginConfig {
680-
disabled: false,
681-
plugin: None,
682680
version: Some(UnresolvedVersionSpec::parse("1.2.3").unwrap()),
683681
config: BTreeMap::from_iter([("custom".into(), serde_json::Value::Bool(true))]),
682+
..Default::default()
684683
})
685684
);
686685
}
@@ -794,13 +793,11 @@ workspace:
794793
plugins: FxHashMap::from_iter([(
795794
Id::raw("typescript"),
796795
ProjectToolchainEntry::Config(ToolchainPluginConfig {
797-
disabled: false,
798-
plugin: None,
799-
version: None,
800796
config: BTreeMap::from_iter([(
801797
"includeSharedTypes".into(),
802798
serde_json::Value::Bool(true)
803799
)]),
800+
..Default::default()
804801
})
805802
)]),
806803
..Default::default()

crates/config/tests/toolchain_config_test.rs

Lines changed: 140 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ mod utils;
33
use httpmock::prelude::*;
44
use moon_config::{
55
BinConfig, BinEntry, ConfigLoader, NodePackageManager, NodeVersionFormat, ToolchainConfig,
6+
ToolchainPluginConfig,
67
};
7-
use proto_core::{Id, ProtoConfig, UnresolvedVersionSpec};
8+
use proto_core::{Id, PluginLocator, ProtoConfig, UnresolvedVersionSpec, warpgate::FileLocator};
89
use schematic::ConfigLoader as BaseLoader;
910
use serde_json::Value;
1011
use serial_test::serial;
@@ -1373,6 +1374,144 @@ typescript:
13731374
}
13741375
}
13751376

1377+
mod plugin {
1378+
use super::*;
1379+
1380+
#[test]
1381+
fn uses_defaults() {
1382+
let config = test_load_config(
1383+
FILENAME,
1384+
r"
1385+
plugin:
1386+
plugin: file://example.wasm
1387+
",
1388+
|path| load_config_from_root(path, &ProtoConfig::default()),
1389+
);
1390+
1391+
let cfg = config.plugins.get("plugin").unwrap();
1392+
1393+
assert_eq!(
1394+
cfg,
1395+
&ToolchainPluginConfig {
1396+
plugin: Some(PluginLocator::File(Box::new(FileLocator {
1397+
file: "file://example.wasm".into(),
1398+
path: None
1399+
}))),
1400+
..Default::default()
1401+
}
1402+
);
1403+
}
1404+
1405+
#[test]
1406+
fn inherits_proto_version() {
1407+
let config = test_load_config(
1408+
FILENAME,
1409+
r"
1410+
plugin:
1411+
plugin: file://example.wasm
1412+
",
1413+
|path| {
1414+
let mut proto = ProtoConfig::default();
1415+
proto.versions.insert(
1416+
Id::raw("plugin"),
1417+
UnresolvedVersionSpec::parse("1.0.0").unwrap().into(),
1418+
);
1419+
1420+
load_config_from_root(path, &proto)
1421+
},
1422+
);
1423+
1424+
let cfg = config.plugins.get("plugin").unwrap();
1425+
1426+
assert_eq!(
1427+
cfg.version.as_ref().unwrap(),
1428+
&UnresolvedVersionSpec::parse("1.0.0").unwrap()
1429+
);
1430+
}
1431+
1432+
#[test]
1433+
fn inherits_proto_version_with_different_id() {
1434+
let config = test_load_config(
1435+
FILENAME,
1436+
r"
1437+
plugin:
1438+
plugin: file://example.wasm
1439+
versionFromPrototools: 'plugin-other'
1440+
",
1441+
|path| {
1442+
let mut proto = ProtoConfig::default();
1443+
proto.versions.insert(
1444+
Id::raw("plugin-other"),
1445+
UnresolvedVersionSpec::parse("1.0.0").unwrap().into(),
1446+
);
1447+
1448+
load_config_from_root(path, &proto)
1449+
},
1450+
);
1451+
1452+
let cfg = config.plugins.get("plugin").unwrap();
1453+
1454+
assert_eq!(
1455+
cfg.version.as_ref().unwrap(),
1456+
&UnresolvedVersionSpec::parse("1.0.0").unwrap()
1457+
);
1458+
}
1459+
1460+
#[test]
1461+
fn doesnt_inherit_proto_version_when_disabled() {
1462+
let config = test_load_config(
1463+
FILENAME,
1464+
r"
1465+
plugin:
1466+
plugin: file://example.wasm
1467+
versionFromPrototools: false
1468+
",
1469+
|path| {
1470+
let mut proto = ProtoConfig::default();
1471+
proto.versions.insert(
1472+
Id::raw("plugin"),
1473+
UnresolvedVersionSpec::parse("1.0.0").unwrap().into(),
1474+
);
1475+
1476+
load_config_from_root(path, &proto)
1477+
},
1478+
);
1479+
1480+
let cfg = config.plugins.get("plugin").unwrap();
1481+
1482+
assert!(cfg.version.is_none());
1483+
}
1484+
1485+
#[test]
1486+
#[serial]
1487+
fn proto_version_doesnt_override() {
1488+
let config = test_load_config(
1489+
FILENAME,
1490+
r"
1491+
plugin:
1492+
plugin: file://example.wasm
1493+
version: 1.0.0
1494+
",
1495+
|path| {
1496+
let mut proto = ProtoConfig::default();
1497+
proto.versions.insert(
1498+
Id::raw("plugin"),
1499+
UnresolvedVersionSpec::parse("2.0.0").unwrap().into(),
1500+
);
1501+
1502+
load_config_from_root(path, &proto)
1503+
},
1504+
);
1505+
1506+
let cfg = config.plugins.get("plugin").unwrap();
1507+
1508+
assert_eq!(
1509+
cfg.version.as_ref().unwrap(),
1510+
&UnresolvedVersionSpec::parse("1.0.0").unwrap()
1511+
);
1512+
}
1513+
}
1514+
13761515
mod pkl {
13771516
use super::*;
13781517
use moon_config::*;

packages/types/src/project-config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ export interface ProjectToolchainCommonToolConfig {
147147
version: UnresolvedVersionSpec | null;
148148
}
149149

150+
export type ToolchainPluginVersionFrom = boolean | string;
151+
150152
export type ProjectToolchainEntry = null | boolean | ToolchainPluginConfig;
151153

152154
/** Overrides top-level toolchain settings, scoped to this project. */

packages/types/src/toolchain-config.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ export interface NodeConfig {
263263
yarn: YarnConfig | null;
264264
}
265265

266+
export type ToolchainPluginVersionFrom = boolean | string;
267+
266268
/** Configures an individual toolchain. */
267269
export interface ToolchainPluginConfig {
268270
/** Arbitrary configuration that'll be passed to the WASM plugin. */
@@ -272,6 +274,12 @@ export interface ToolchainPluginConfig {
272274
plugin: PluginLocator | null;
273275
/** The version of the toolchain to download and install. */
274276
version: UnresolvedVersionSpec | null;
277+
/**
278+
* Inherit the version from the root `.prototools`.
279+
* When true, matches using the same ID, otherwise a
280+
* string can be provided for a custom ID.
281+
*/
282+
versionFromPrototools: ToolchainPluginVersionFrom;
275283
}
276284

277285
/** The available package managers for Python. */
@@ -626,6 +634,12 @@ export interface PartialToolchainPluginConfig {
626634
plugin?: PluginLocator | null;
627635
/** The version of the toolchain to download and install. */
628636
version?: UnresolvedVersionSpec | null;
637+
/**
638+
* Inherit the version from the root `.prototools`.
639+
* When true, matches using the same ID, otherwise a
640+
* string can be provided for a custom ID.
641+
*/
642+
versionFromPrototools?: ToolchainPluginVersionFrom | null;
629643
}
630644

631645
export interface PartialPipConfig {

website/static/schemas/project.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1613,10 +1613,32 @@
16131613
"type": "null"
16141614
}
16151615
]
1616+
},
1617+
"versionFromPrototools": {
1618+
"title": "versionFromPrototools",
1619+
"description": "Inherit the version from the root .prototools. When true, matches using the same ID, otherwise a string can be provided for a custom ID.",
1620+
"allOf": [
1621+
{
1622+
"$ref": "#/definitions/ToolchainPluginVersionFrom"
1623+
}
1624+
],
1625+
"markdownDescription": "Inherit the version from the root `.prototools`. When true, matches using the same ID, otherwise a string can be provided for a custom ID."
16161626
}
16171627
},
16181628
"additionalProperties": false
16191629
},
1630+
"ToolchainPluginVersionFrom": {
1631+
"description": "Strategy in which to inherit a version from .prototools.",
1632+
"anyOf": [
1633+
{
1634+
"type": "boolean"
1635+
},
1636+
{
1637+
"type": "string"
1638+
}
1639+
],
1640+
"markdownDescription": "Strategy in which to inherit a version from `.prototools`."
1641+
},
16201642
"UnresolvedVersionSpec": {
16211643
"description": "Represents an unresolved version or alias that must be resolved to a fully-qualified version.",
16221644
"type": "string",

0 commit comments

Comments
 (0)