-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(pacmak): .NET build downloading packages from NuGet (#949)
Fix situation where the .NET build accidentally downloads dependency packages from NuGet, instead of using the locally built packages. Caused by packages being built in the order given on the command line, and if a consumer is built before a dependency, the NuGet build would fall back to loading the package from the online repository. Apply a topological sort to package building to prevent this from happening.
- Loading branch information
Showing
6 changed files
with
148 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
export type KeyFunc<T> = (x: T) => string; | ||
export type DepFunc<T> = (x: T) => string[]; | ||
|
||
/** | ||
* Return a topological sort of all elements of xs, according to the given dependency functions | ||
* | ||
* Dependencies outside the referenced set are ignored. | ||
* | ||
* Not a stable sort, but in order to keep the order as stable as possible, we'll sort by key | ||
* among elements of equal precedence. | ||
* | ||
* @param xs - The elements to sort | ||
* @param keyFn - Return an element's identifier | ||
* @param depFn - Return the identifiers of an element's dependencies | ||
*/ | ||
export function topologicalSort<T>(xs: Iterable<T>, keyFn: KeyFunc<T>, depFn: DepFunc<T>): T[] { | ||
const remaining = new Map<string, TopoElement<T>>(); | ||
for (const element of xs) { | ||
const key = keyFn(element); | ||
remaining.set(key, { key, element, dependencies: depFn(element) }); | ||
} | ||
|
||
const ret = new Array<T>(); | ||
while (remaining.size > 0) { | ||
// All elements with no more deps in the set can be ordered | ||
const selectable = Array.from(remaining.values()).filter(e => e.dependencies.every(d => !remaining.has(d))); | ||
|
||
selectable.sort((a, b) => a.key < b.key ? -1 : b.key < a.key ? 1 : 0); | ||
|
||
for (const selected of selectable) { | ||
ret.push(selected.element); | ||
remaining.delete(selected.key); | ||
} | ||
|
||
// If we didn't make any progress, we got stuck | ||
if (selectable.length === 0) { | ||
throw new Error(`Could not determine ordering between: ${Array.from(remaining.keys()).join(', ')}`); | ||
} | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
interface TopoElement<T> { | ||
key: string; | ||
dependencies: string[]; | ||
element: T; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import mockfs = require('mock-fs'); | ||
import { findJsiiModules } from '../lib/npm-modules'; | ||
|
||
test('findJsiiModules is sorted topologically', async () => { | ||
mockfs({ | ||
'/packageA/package.json': JSON.stringify({ | ||
name: 'packageA', | ||
jsii: { | ||
outdir: 'dist', | ||
targets: { | ||
python: {} | ||
} | ||
}, | ||
dependencies: { | ||
packageB: '*' | ||
} | ||
}), | ||
'/packageB/package.json': JSON.stringify({ | ||
name: 'packageB', | ||
jsii: { | ||
outdir: 'dist', | ||
targets: { | ||
python: {} | ||
} | ||
} | ||
}), | ||
}); | ||
|
||
try { | ||
const mods = await findJsiiModules(['/packageA', '/packageB'], false); | ||
expect(mods.map(m => m.name)).toEqual(['packageB', 'packageA']); | ||
} finally { | ||
mockfs.restore(); | ||
} | ||
}); | ||
|
||
test('findJsiiModules without deps loads packages in given order', async () => { | ||
mockfs({ | ||
'/packageA/package.json': JSON.stringify({ | ||
name: 'packageA', | ||
jsii: { | ||
outdir: 'dist', | ||
targets: { | ||
python: {} | ||
} | ||
}, | ||
}), | ||
'/packageB/package.json': JSON.stringify({ | ||
name: 'packageB', | ||
jsii: { | ||
outdir: 'dist', | ||
targets: { | ||
python: {} | ||
} | ||
} | ||
}), | ||
}); | ||
|
||
try { | ||
const mods = await findJsiiModules(['/packageA', '/packageB'], false); | ||
expect(mods.map(m => m.name)).toEqual(['packageA', 'packageB']); | ||
} finally { | ||
mockfs.restore(); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters