Skip to content

Commit 0fc3850

Browse files
authored
handling relative paths in cbuild-run file (#772)
* handling relative paths in cbuild-run file * update tests
1 parent a333b8e commit 0fc3850

6 files changed

Lines changed: 100 additions & 12 deletions

File tree

src/cbuild-run/__snapshots__/cbuild-run-reader.test.ts.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,18 @@ exports[`CbuildRunReader Parser successfully parses a *.cbuild-run.yml file 1`]
133133
"file": "\${CMSIS_PACK_ROOT}/MyVendor/MyDevice/1.0.0/Debug/SVD/MyDevice_generic.svd",
134134
"type": "svd",
135135
},
136+
{
137+
"file": "../../MyDevice/multi-core-custom.svd",
138+
"type": "svd",
139+
},
136140
{
137141
"file": "\${CMSIS_PACK_ROOT}/MyVendor/MyDevice/1.0.0/Debug/SCVD/MySoftware_component.scvd",
138142
"type": "scvd",
139143
},
144+
{
145+
"file": "../../MyDevice/multi-core-custom.scvd",
146+
"type": "scvd",
147+
},
140148
{
141149
"file": "\${CMSIS_PACK_ROOT}/MyVendor/MyDevice/1.0.0/Debug/SCVD/Core1.scvd",
142150
"pname": "Core1",

src/cbuild-run/cbuild-run-reader.test.ts

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2025 Arm Limited
2+
* Copyright 2025-2026 Arm Limited
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,10 +14,16 @@
1414
* limitations under the License.
1515
*/
1616

17+
import * as path from 'path';
1718
import { CbuildRunReader } from './cbuild-run-reader';
1819

1920
const TEST_CBUILD_RUN_FILE = 'test-data/multi-core.cbuild-run.yml'; // Relative to repo root
2021
const TEST_FILE_PATH = 'test-data/fileReaderTest.txt'; // Relative to repo root
22+
const PACK_ROOT = '/my/pack/root';
23+
24+
const EXPECTED_CUSTOM_SVD = path.resolve(path.dirname(TEST_CBUILD_RUN_FILE), '../../MyDevice/multi-core-custom.svd');
25+
const EXPECTED_CUSTOM_SCVD = path.resolve(path.dirname(TEST_CBUILD_RUN_FILE), '../../MyDevice/multi-core-custom.scvd');
26+
2127

2228
describe('CbuildRunReader', () => {
2329

@@ -61,6 +67,7 @@ describe('CbuildRunReader', () => {
6167
'/my/pack/root/MyVendor/MyDevice/1.0.0/Debug/SVD/MyDevice_Core0.svd',
6268
'/my/pack/root/MyVendor/MyDevice/1.0.0/Debug/SVD/MyDevice_Core1.svd',
6369
'/my/pack/root/MyVendor/MyDevice/1.0.0/Debug/SVD/MyDevice_generic.svd',
70+
EXPECTED_CUSTOM_SVD,
6471
]
6572
},
6673
{
@@ -69,6 +76,7 @@ describe('CbuildRunReader', () => {
6976
expectedSvdPaths: [
7077
'/my/pack/root/MyVendor/MyDevice/1.0.0/Debug/SVD/MyDevice_Core0.svd',
7178
'/my/pack/root/MyVendor/MyDevice/1.0.0/Debug/SVD/MyDevice_generic.svd',
79+
EXPECTED_CUSTOM_SVD,
7280
]
7381
},
7482
{
@@ -77,15 +85,19 @@ describe('CbuildRunReader', () => {
7785
expectedSvdPaths: [
7886
'/my/pack/root/MyVendor/MyDevice/1.0.0/Debug/SVD/MyDevice_Core1.svd',
7987
'/my/pack/root/MyVendor/MyDevice/1.0.0/Debug/SVD/MyDevice_generic.svd',
88+
EXPECTED_CUSTOM_SVD,
8089
]
8190
},
8291
])('returns SVD file path ($info)', async ({ pname, expectedSvdPaths }) => {
8392
await cbuildRunReader.parse(TEST_CBUILD_RUN_FILE);
8493
const svdFilePaths = cbuildRunReader.getSvdFilePaths('/my/pack/root', pname);
85-
expect(svdFilePaths.length).toEqual(svdFilePaths.length);
94+
expect(svdFilePaths.length).toEqual(expectedSvdPaths.length);
8695
for (let i = 0; i < svdFilePaths.length; i++) {
8796
// eslint-disable-next-line security/detect-object-injection
88-
expect(expectedSvdPaths[i]).toEqual(svdFilePaths[i]);
97+
const expectedPath = path.normalize(path.resolve(expectedSvdPaths[i]));
98+
// eslint-disable-next-line security/detect-object-injection
99+
const actualPath = path.normalize(path.resolve(svdFilePaths[i]));
100+
expect(expectedPath).toEqual(actualPath);
89101
}
90102
});
91103

@@ -111,11 +123,28 @@ describe('CbuildRunReader', () => {
111123
const systemDescriptions = cbuildRun?.['system-descriptions'];
112124
expect(systemDescriptions).toBeDefined();
113125
const svdPaths = cbuildRunReader.getSvdFilePaths('', 'Core1');
126+
expect(svdPaths.map(path.normalize)).toEqual([
127+
path.normalize(
128+
path.resolve(path.dirname(TEST_CBUILD_RUN_FILE), '${CMSIS_PACK_ROOT}', 'MyVendor', 'MyDevice', '1.0.0', 'Debug', 'SVD', 'MyDevice_Core1.svd')
129+
),
130+
path.normalize(
131+
path.resolve(path.dirname(TEST_CBUILD_RUN_FILE), '${CMSIS_PACK_ROOT}', 'MyVendor', 'MyDevice', '1.0.0', 'Debug', 'SVD', 'MyDevice_generic.svd')
132+
),
133+
path.normalize(EXPECTED_CUSTOM_SVD),
134+
]);
135+
});
114136

115-
expect(svdPaths).toEqual([
116-
'${CMSIS_PACK_ROOT}/MyVendor/MyDevice/1.0.0/Debug/SVD/MyDevice_Core1.svd',
117-
'${CMSIS_PACK_ROOT}/MyVendor/MyDevice/1.0.0/Debug/SVD/MyDevice_generic.svd'
137+
it('includes descriptors without pname when filtering by pname (SVD)', async () => {
138+
await cbuildRunReader.parse(TEST_CBUILD_RUN_FILE);
139+
const cbuildRun = (cbuildRunReader as unknown as { cbuildRun?: { ['system-descriptions']?: Array<{ file: string; type: string; pname?: string }> } }).cbuildRun;
140+
const systemDescriptions = cbuildRun?.['system-descriptions'];
141+
expect(systemDescriptions).toBeDefined();
142+
const svdPaths = cbuildRunReader.getSvdFilePaths(PACK_ROOT, 'Core1');
118143

144+
expect(svdPaths.map(path.normalize)).toEqual([
145+
path.normalize(path.resolve(PACK_ROOT, 'MyVendor', 'MyDevice', '1.0.0', 'Debug', 'SVD', 'MyDevice_Core1.svd')),
146+
path.normalize(path.resolve(PACK_ROOT, 'MyVendor', 'MyDevice', '1.0.0', 'Debug', 'SVD', 'MyDevice_generic.svd')),
147+
path.normalize(EXPECTED_CUSTOM_SVD),
119148
]);
120149
});
121150

@@ -124,12 +153,29 @@ describe('CbuildRunReader', () => {
124153
const cbuildRun = (cbuildRunReader as unknown as { cbuildRun?: { ['system-descriptions']?: Array<{ file: string; type: string; pname?: string }> } }).cbuildRun;
125154
const systemDescriptions = cbuildRun?.['system-descriptions'] ?? [];
126155
expect(systemDescriptions).toBeDefined();
127-
const scvdPaths = cbuildRunReader.getScvdFilePaths('', 'Core1');
156+
const scvdPaths = cbuildRunReader.getScvdFilePaths(PACK_ROOT, 'Core1');
128157

129158
expect(scvdPaths).toEqual([
130-
'${CMSIS_PACK_ROOT}/MyVendor/MyDevice/1.0.0/Debug/SCVD/MySoftware_component.scvd',
131-
'${CMSIS_PACK_ROOT}/MyVendor/MyDevice/1.0.0/Debug/SCVD/Core1.scvd',
159+
path.normalize(path.resolve(PACK_ROOT, 'MyVendor', 'MyDevice', '1.0.0', 'Debug', 'SCVD', 'MySoftware_component.scvd')),
160+
path.normalize(EXPECTED_CUSTOM_SCVD),
161+
path.normalize(path.resolve(PACK_ROOT, 'MyVendor', 'MyDevice', '1.0.0', 'Debug', 'SCVD', 'Core1.scvd')),
132162
]);
133163
});
164+
165+
it('resolves relative SVD paths relative to the cbuild-run.yml file location', async () => {
166+
await cbuildRunReader.parse(TEST_CBUILD_RUN_FILE);
167+
const svdFilePaths = cbuildRunReader.getSvdFilePaths(PACK_ROOT);
168+
const expectedTail = path.normalize(path.join('MyDevice', 'multi-core-custom.svd'));
169+
const resolvedCustom = svdFilePaths.find((p: string) => p.endsWith(expectedTail));
170+
expect(resolvedCustom).toEqual(path.normalize(EXPECTED_CUSTOM_SVD));
171+
});
172+
173+
it('resolves relative SCVD paths relative to the cbuild-run.yml file location', async () => {
174+
await cbuildRunReader.parse(TEST_CBUILD_RUN_FILE);
175+
const scvdFilePaths = cbuildRunReader.getScvdFilePaths(PACK_ROOT);
176+
const expectedTail = path.normalize(path.join('MyDevice', 'multi-core-custom.scvd'));
177+
const resolvedCustom = scvdFilePaths.find((p: string) => p.endsWith(expectedTail));
178+
expect(resolvedCustom).toEqual(path.normalize(EXPECTED_CUSTOM_SCVD));
179+
});
134180
});
135181
});

src/cbuild-run/cbuild-run-reader.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2025 Arm Limited
2+
* Copyright 2025-2026 Arm Limited
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import * as yaml from 'yaml';
18+
import * as path from 'path';
1819
import { CbuildRunRootType, CbuildRunType } from './cbuild-run-types';
1920
import { FileReader, VscodeFileReader } from '../desktop/file-reader';
2021
import { getCmsisPackRootPath } from '../utils';
@@ -23,6 +24,8 @@ const CMSIS_PACK_ROOT_ENVVAR = '${CMSIS_PACK_ROOT}';
2324

2425
export class CbuildRunReader {
2526
private cbuildRun: CbuildRunType | undefined;
27+
private cbuildRunFilePath: string | undefined;
28+
private cbuildRunDir: string | undefined;
2629

2730
constructor(private reader: FileReader = new VscodeFileReader()) {}
2831

@@ -41,6 +44,8 @@ export class CbuildRunReader {
4144
if (!this.cbuildRun) {
4245
throw new Error(`Invalid '*.cbuild-run.yml' file: ${filePath}`);
4346
}
47+
this.cbuildRunFilePath = filePath;
48+
this.cbuildRunDir = path.dirname(this.cbuildRunFilePath);
4449
}
4550

4651
public getSvdFilePaths(cmsisPackRoot?: string, pname?: string): string[] {
@@ -72,10 +77,19 @@ export class CbuildRunReader {
7277
}
7378
return descriptor.pname === pname;
7479
}): fileDescriptors;
80+
// Get file paths, they might be relative paths
7581
const filePaths = filteredDescriptors.map(descriptor => `${effectiveCmsisPackRoot
7682
? descriptor.file.replaceAll(CMSIS_PACK_ROOT_ENVVAR, effectiveCmsisPackRoot)
7783
: descriptor.file}`);
78-
return filePaths;
84+
// resolve relative paths to cbuild run file location
85+
const resolvedRelativeFilePaths = filePaths.map(filePath => {
86+
if (path.isAbsolute(filePath) || !this.cbuildRunDir) {
87+
return filePath;
88+
}
89+
return path.join(this.cbuildRunDir, filePath);
90+
});
91+
const resolvedFilePaths = resolvedRelativeFilePaths.map(filePath => path.resolve(filePath));
92+
return resolvedFilePaths;
7993
}
8094

8195
public getPnames(): string[] {

src/debug-configuration/subproviders/generic-configuration-provider.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17+
import path from 'path';
18+
1719
import { gdbTargetConfiguration, targetConfigurationFactory } from '../debug-configuration.factory';
1820
import { ExtendedGDBTargetConfiguration } from '../gdbtarget-configuration';
1921
import { GenericConfigurationProvider } from './generic-configuration-provider';
@@ -48,7 +50,11 @@ describe('GenericConfigurationProvider', () => {
4850
});
4951
const debugConfig = await configProvider.resolveDebugConfigurationWithSubstitutedVariables(undefined, config, undefined);
5052
const gdbTargetConfig = debugConfig as ExtendedGDBTargetConfiguration;
51-
expect(gdbTargetConfig.definitionPath?.endsWith(expectedSvdPath)).toBeTruthy();
53+
const normalizedDefinitionPath = gdbTargetConfig.definitionPath
54+
? path.normalize(gdbTargetConfig.definitionPath)
55+
: undefined;
56+
const normalizedExpectedPath = path.normalize(expectedSvdPath);
57+
expect(normalizedDefinitionPath?.endsWith(normalizedExpectedPath)).toBeTruthy();
5258
});
5359

5460
});

src/debug-session/__snapshots__/gdbtarget-debug-session.test.ts.snap

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,18 @@ CbuildRunReader {
134134
"file": "\${CMSIS_PACK_ROOT}/MyVendor/MyDevice/1.0.0/Debug/SVD/MyDevice_generic.svd",
135135
"type": "svd",
136136
},
137+
{
138+
"file": "../../MyDevice/multi-core-custom.svd",
139+
"type": "svd",
140+
},
137141
{
138142
"file": "\${CMSIS_PACK_ROOT}/MyVendor/MyDevice/1.0.0/Debug/SCVD/MySoftware_component.scvd",
139143
"type": "scvd",
140144
},
145+
{
146+
"file": "../../MyDevice/multi-core-custom.scvd",
147+
"type": "scvd",
148+
},
141149
{
142150
"file": "\${CMSIS_PACK_ROOT}/MyVendor/MyDevice/1.0.0/Debug/SCVD/Core1.scvd",
143151
"pname": "Core1",
@@ -174,6 +182,8 @@ CbuildRunReader {
174182
},
175183
"target-type": "My-Test-Target-Type",
176184
},
185+
"cbuildRunDir": "test-data",
186+
"cbuildRunFilePath": "test-data/multi-core.cbuild-run.yml",
177187
"reader": VscodeFileReader {},
178188
}
179189
`;

test-data/multi-core.cbuild-run.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,12 @@ cbuild-run:
4040
pname: Core1
4141
- file: ${CMSIS_PACK_ROOT}/MyVendor/MyDevice/1.0.0/Debug/SVD/MyDevice_generic.svd
4242
type: svd
43+
- file: ../../MyDevice/multi-core-custom.svd
44+
type: svd
4345
- file: ${CMSIS_PACK_ROOT}/MyVendor/MyDevice/1.0.0/Debug/SCVD/MySoftware_component.scvd
4446
type: scvd
47+
- file: ../../MyDevice/multi-core-custom.scvd
48+
type: scvd
4549
- file: ${CMSIS_PACK_ROOT}/MyVendor/MyDevice/1.0.0/Debug/SCVD/Core1.scvd
4650
type: scvd
4751
pname: Core1

0 commit comments

Comments
 (0)