Skip to content

Commit d5fffec

Browse files
committed
Only add xcode-tools on macOS when a resource requires it
Replace the unconditional xcode-tools injection with Project.addRequiredResources(): it adds a default config for every type a resource requires (ResourceSettings.requires) that the config doesn't declare, transitively (cursor -> homebrew -> xcode-tools), before dependencies are resolved. Required types also become dependencies, which fixes soft dependencies on xcode-tools never being wired (the injection ran after dependency resolution). Plugins built before plugin-core 2.1 don't send requires: on macOS their resources still require xcode-tools. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016QnBk1brshJrHGEkhMFR29
1 parent 6c7eaaa commit d5fffec

5 files changed

Lines changed: 158 additions & 17 deletions

File tree

‎CLAUDE.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ The `test` command spins up a Tart VM to test Codify configs in isolation:
7474
6. **Dependency Resolution**:
7575
- Explicit: `dependsOn` field in config
7676
- Implicit: Extracted from parameter references (e.g., `${other-resource.param}`)
77-
- Plugin-level: Plugins declare type dependencies (e.g., xcode-tools on macOS)
77+
- Plugin-level: Plugins declare type dependencies (soft: only order resources that are present)
78+
- Required: `requires` in a resource definition (ex: `homebrew`, `xcode-tools` on macOS). `Project.addRequiredResources()` adds a default config for missing required types, transitively, before dependencies are resolved
7879
- Topological sort ensures correct evaluation order (`src/utils/dependency-graph-resolver.ts`)
7980

8081
### Key Directory Structure
@@ -147,7 +148,7 @@ Parent Process Plugin Process
147148

148149
1. **Single file Projects**: Projects only currently support one file
149150
2. **Cloud-First**: UUIDs are valid "file paths" - enables seamless local/cloud switching
150-
3. **XCode Tools Injection**: On macOS, `xcode-tools` automatically prepended (most resources depend on it)
151+
3. **Required resources instead of always installing Xcode Command Line Tools**: `xcode-tools` (and `homebrew`) are only added when a resource `requires` them. Definitions without `requires` (plugins built before plugin-core 2.1) require `xcode-tools` on macOS, like before
151152
4. **Test VM Strategy**: Uses Tart VMs with bind mounts (not copying) + file watcher for live config editing
152153
5. **OS Filtering**: Resources specify `os: ["Darwin", "Linux"]` for conditional inclusion
153154
6. **Secure Mode**: `--secure` flag forces sudo prompt for every command (no password caching)

‎src/entities/project.ts‎

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -228,14 +228,33 @@ ${JSON.stringify(projectConfigs, null, 2)}`);
228228
return this.resourceConfigs.find((r) => r.isSame(type, name)) ?? null;
229229
}
230230

231-
addXCodeToolsConfig() {
232-
this.resourceConfigs.unshift(new ResourceConfig({
233-
type: 'xcode-tools'
234-
}));
235-
236-
if (this.evaluationOrder) {
237-
this.evaluationOrder.unshift('xcode-tools');
231+
/**
232+
* Adds a default config (ex: { type: 'homebrew' }) for every type that a resource requires but the project doesn't
233+
* declare, transitively (cursor requires homebrew, which requires xcode-tools). Must run before
234+
* {@link resolveDependenciesAndCalculateEvalOrder}: required resources become dependencies of the resources that need
235+
* them. Returns the added resources.
236+
*/
237+
addRequiredResources(resourceDefinitions: ResourceDefinitionMap): ResourceConfig[] {
238+
const presentTypes = new Set(this.resourceConfigs.map((r) => r.type));
239+
const added: ResourceConfig[] = [];
240+
const queue = [...this.resourceConfigs];
241+
242+
while (queue.length > 0) {
243+
const resource = queue.shift()!;
244+
245+
const missingTypes = requiredTypes(resource.type, resourceDefinitions).filter((type) => !presentTypes.has(type));
246+
for (const type of missingTypes) {
247+
const required = new ResourceConfig({ type });
248+
presentTypes.add(type);
249+
added.push(required);
250+
queue.push(required);
251+
ctx.debug(`Adding "${type}" because it is required by "${resource.id}"`);
252+
}
238253
}
254+
255+
// Prerequisites of prerequisites go first (xcode-tools before homebrew) so the stable sort keeps them in that order.
256+
this.resourceConfigs.unshift(...[...added].reverse());
257+
return added;
239258
}
240259

241260
validateTypeIds(resourceDefinitions: ResourceDefinitionMap) {
@@ -402,9 +421,14 @@ ${JSON.stringify(projectConfigs, null, 2)}`);
402421
r.addDependenciesBasedOnParameters((id) => resourceMap.has(id));
403422

404423
// Plugin dependencies are soft dependencies. They only activate if the dependent resource is present.
405-
r.addDependencies(resourceDefinitions?.get(r.type)?.dependencies
406-
?.filter((type) => [...resourceMap.values()].some((r) => r.type === type))
407-
?.flatMap((type) => [...resourceMap.values()].filter((r) => r.type === type).map((r) => r.id)) ?? []
424+
// Required types are added by addRequiredResources(), so they are present here unless their plugin isn't loaded.
425+
const dependencyTypes = new Set([
426+
...resourceDefinitions?.get(r.type)?.dependencies ?? [],
427+
...(resourceDefinitions ? requiredTypes(r.type, resourceDefinitions) : []),
428+
]);
429+
r.addDependencies([...resourceMap.values()]
430+
.filter((other) => other.id !== r.id && dependencyTypes.has(other.type))
431+
.map((other) => other.id)
408432
);
409433
}
410434
}
@@ -555,3 +579,20 @@ ${JSON.stringify(projectConfigs, null, 2)}`);
555579
}
556580
}
557581
}
582+
583+
/**
584+
* Types that must exist for a resource of this type to work (ResourceSettings.requires in plugin-core). Plugins built
585+
* before plugin-core 2.1 don't send requires: on macOS their resources keep the previous behaviour of always requiring
586+
* xcode-tools.
587+
*/
588+
function requiredTypes(type: string, resourceDefinitions: ResourceDefinitionMap): string[] {
589+
const definition = resourceDefinitions.get(type) as (undefined | { requires?: string[] });
590+
if (!definition) {
591+
return [];
592+
}
593+
594+
const requires = definition.requires
595+
?? (OsUtils.isMacOS() ? ['xcode-tools'] : []);
596+
597+
return [...new Set(requires)].filter((t) => t !== type && resourceDefinitions.has(t));
598+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import { ResourceDefinition } from '@codifycli/schemas';
2+
import { afterEach, describe, expect, it, vi } from 'vitest';
3+
4+
import { ResourceDefinitionMap } from '../plugins/plugin-manager.js';
5+
import { OsUtils } from '../utils/os-utils.js';
6+
import { Project } from './project.js';
7+
import { ResourceConfig } from './resource-config.js';
8+
9+
function definitions(defs: Record<string, { dependencies?: string[]; requires?: string[] }>): ResourceDefinitionMap {
10+
return new Map(Object.entries(defs).map(([type, d]) => [
11+
type,
12+
{ type, dependencies: d.dependencies ?? [], ...('requires' in d ? { requires: d.requires } : {}) } as ResourceDefinition,
13+
]));
14+
}
15+
16+
function plan(defs: ResourceDefinitionMap, ...configs: ResourceConfig[]) {
17+
const project = new Project(null, configs, []);
18+
const added = project.addRequiredResources(defs);
19+
project.resolveDependenciesAndCalculateEvalOrder(defs);
20+
return { added: added.map((r) => r.id), order: project.evaluationOrder!, project };
21+
}
22+
23+
const DEFS = definitions({
24+
'xcode-tools': { requires: [] },
25+
homebrew: { requires: ['xcode-tools'] },
26+
cursor: { requires: ['homebrew'] },
27+
'git-repository': { requires: ['xcode-tools'] },
28+
'macos-settings': { requires: [] },
29+
alias: { requires: [] },
30+
});
31+
32+
describe('Required resources', () => {
33+
afterEach(() => {
34+
vi.restoreAllMocks();
35+
});
36+
37+
it('adds nothing for resources that require nothing', () => {
38+
const { added, order } = plan(DEFS, new ResourceConfig({ type: 'macos-settings' }), new ResourceConfig({ type: 'alias', alias: 'll' }));
39+
40+
expect(added).toEqual([]);
41+
expect(order).toEqual(['macos-settings', 'alias']);
42+
});
43+
44+
it('adds required resources transitively and applies them first', () => {
45+
const { added, order } = plan(DEFS, new ResourceConfig({ type: 'alias' }), new ResourceConfig({ type: 'cursor' }));
46+
47+
expect(added).toEqual(['homebrew', 'xcode-tools']);
48+
expect(order.indexOf('xcode-tools')).toBeLessThan(order.indexOf('homebrew'));
49+
expect(order.indexOf('homebrew')).toBeLessThan(order.indexOf('cursor'));
50+
});
51+
52+
it('adds a required type once, even when several resources require it', () => {
53+
const { added, project } = plan(
54+
DEFS,
55+
new ResourceConfig({ type: 'git-repository', name: 'a' }),
56+
new ResourceConfig({ type: 'git-repository', name: 'b' }),
57+
new ResourceConfig({ type: 'homebrew' }),
58+
);
59+
60+
expect(added).toEqual(['xcode-tools']);
61+
expect(project.findAll('xcode-tools')).toHaveLength(1);
62+
expect(project.findSpecific('git-repository', 'a')!.dependencyIds).toContain('xcode-tools');
63+
expect(project.findSpecific('git-repository', 'b')!.dependencyIds).toContain('xcode-tools');
64+
});
65+
66+
it('uses the declared resource instead of adding one, and makes it a dependency', () => {
67+
const declaredBrew = new ResourceConfig({ type: 'homebrew', formulae: ['jq'] });
68+
const cursor = new ResourceConfig({ type: 'cursor' });
69+
const { added, order } = plan(DEFS, cursor, declaredBrew);
70+
71+
expect(added).toEqual(['xcode-tools']);
72+
expect(cursor.dependencyIds).toEqual(['homebrew']);
73+
expect(order.indexOf('homebrew')).toBeLessThan(order.indexOf('cursor'));
74+
});
75+
76+
it('ignores required types that no loaded plugin defines', () => {
77+
const defs = definitions({ thing: { requires: ['unknown-type'] } });
78+
const { added } = plan(defs, new ResourceConfig({ type: 'thing' }));
79+
80+
expect(added).toEqual([]);
81+
});
82+
83+
it('requires xcode-tools on macOS for plugins that do not send requires (built before plugin-core 2.1)', () => {
84+
vi.spyOn(OsUtils, 'isMacOS').mockReturnValue(true);
85+
const defs = definitions({ 'xcode-tools': {}, 'macos-settings': {} });
86+
87+
const { added, order } = plan(defs, new ResourceConfig({ type: 'macos-settings' }));
88+
89+
expect(added).toEqual(['xcode-tools']);
90+
expect(order).toEqual(['xcode-tools', 'macos-settings']);
91+
});
92+
93+
it('does not add xcode-tools for older plugins on Linux', () => {
94+
vi.spyOn(OsUtils, 'isMacOS').mockReturnValue(false);
95+
const defs = definitions({ 'xcode-tools': {}, alias: {} });
96+
97+
const { added } = plan(defs, new ResourceConfig({ type: 'alias' }));
98+
99+
expect(added).toEqual([]);
100+
});
101+
});

‎src/notices/notice-evaluator.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const CONTROL_CHARACTERS = /[\u{0}-\u{8}\u{B}-\u{1F}\u{7F}-\u{9F}]/gu;
3434
export const NoticeEvaluator = {
3535
/**
3636
* Resolves the resources of every notice. Must run once the resources are final (after the os/distro filters, the
37-
* template merge and the xcode-tools injection). Throws for dependsOn references that don't exist.
37+
* template merge and the required resources). Throws for dependsOn references that don't exist.
3838
*/
3939
resolve(project: Project): Map<NoticeConfig, string[]> {
4040
const result = new Map<NoticeConfig, string[]>();

‎src/orchestrators/plan.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { TemplateCommandApproval } from '../templates/template-command-approval.
1111
import { MergeWarning } from '../templates/template-resource-merger.js';
1212
import { Reporter } from '../ui/reporters/reporter.js';
1313
import { createStartupShellScriptsIfNotExists } from '../utils/file.js';
14-
import { OsUtils } from '../utils/os-utils.js';
1514
import { ValidateOrchestrator } from './validate.js';
1615
import { CodifySourceType, type TargetKind } from '../parser/resolver.js';
1716
import type { ResolvedVariable } from '../variables/variable-resolver.js';
@@ -61,10 +60,9 @@ export class PlanOrchestrator {
6160
// Prompt for missing required values and dashboard placeholders. Must happen before validation.
6261
await MissingParametersResolver.run(initializationResult, reporter, args.presetParameterValues);
6362
await ValidateOrchestrator.run({ existing: initializationResult, noProgress: args.noProgress }, reporter);
63+
// Ex: homebrew for resources installed with brew, xcode-tools for resources that need git or a compiler on macOS
64+
project.addRequiredResources(resourceDefinitions);
6465
project.resolveDependenciesAndCalculateEvalOrder(resourceDefinitions);
65-
if (OsUtils.isMacOS()) {
66-
project.addXCodeToolsConfig(); // We have to add xcode-tools config always to MacOS since almost every resource depends on it
67-
}
6866

6967
// Also validates the dependsOn of every notice before planning
7068
const planNotices = NoticeEvaluator.forPlan(project);

0 commit comments

Comments
 (0)