Skip to content

Commit 85e2024

Browse files
committed
don't delete dependencies when merging upstream changes (#10484)
1 parent 3123906 commit 85e2024

File tree

1 file changed

+37
-28
lines changed

1 file changed

+37
-28
lines changed

webapp/src/workspace.ts

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,52 +1273,61 @@ async function githubUpdateToAsync(hd: Header, options: UpdateOptions) {
12731273
}
12741274

12751275
// we need to keep the old cfg to maintain the github id -> local workspace id
1276-
const oldCfg = pxt.Package.parseAndValidConfig(files[pxt.CONFIG_NAME])
1276+
const localConfig = pxt.Package.parseAndValidConfig(files[pxt.CONFIG_NAME])
12771277
const cfgText = await downloadAsync(pxt.CONFIG_NAME)
1278-
let cfg = pxt.Package.parseAndValidConfig(cfgText);
1278+
let pulledConfig = pxt.Package.parseAndValidConfig(cfgText);
12791279
// invalid cfg
1280-
if (!cfg) {
1280+
if (!pulledConfig) {
12811281
if (hd) // not importing
12821282
U.userError(lf("Invalid pxt.json file."));
12831283
pxt.debug(`github: reconstructing pxt.json`)
1284-
cfg = pxt.diff.reconstructConfig(parsed, files, commit, pxt.appTarget.blocksprj || pxt.appTarget.tsprj);
1284+
pulledConfig = pxt.diff.reconstructConfig(parsed, files, commit, pxt.appTarget.blocksprj || pxt.appTarget.tsprj);
12851285
if (parsed.fileName && parsed.project)
12861286
// add root folder as reference when creating nested project
1287-
cfg.dependencies[parsed.project] = `github:${parsed.slug}`
1288-
files[pxt.CONFIG_NAME] = pxt.Package.stringifyConfig(cfg);
1287+
pulledConfig.dependencies[parsed.project] = `github:${parsed.slug}`
1288+
files[pxt.CONFIG_NAME] = pxt.Package.stringifyConfig(pulledConfig);
12891289
}
12901290
// patch the github references back to local workspaces
1291-
if (oldCfg) {
1291+
if (localConfig) {
12921292
let ghupdated = 0;
1293-
Object.keys(cfg.dependencies)
1294-
// find github references that are also in the original version
1295-
.filter(k => /^github:/.test(cfg.dependencies[k]) && oldCfg.dependencies[k])
1296-
.forEach(k => {
1297-
const gid = pxt.github.parseRepoId(cfg.dependencies[k]);
1298-
if (gid) {
1299-
const wks = oldCfg.dependencies[k];
1300-
cfg.dependencies[k] = wks;
1301-
ghupdated++;
1293+
1294+
for (const dep of Object.keys(pulledConfig.dependencies)) {
1295+
if (!(/^github:/.test(pulledConfig.dependencies[dep]) && localConfig.dependencies[dep])) {
1296+
continue;
1297+
}
1298+
const gid = pxt.github.parseRepoId(pulledConfig.dependencies[dep]);
1299+
if (gid) {
1300+
const wks = localConfig.dependencies[dep];
1301+
pulledConfig.dependencies[dep] = wks;
1302+
ghupdated++;
1303+
}
1304+
}
1305+
1306+
if (ghupdated) {
1307+
// merge in any locally added dependencies
1308+
for (const dep of Object.keys(localConfig.dependencies)) {
1309+
if (!pulledConfig.dependencies[dep]) {
1310+
pulledConfig.dependencies[dep] = localConfig.dependencies[dep];
13021311
}
1303-
})
1304-
if (ghupdated)
1305-
files[pxt.CONFIG_NAME] = pxt.Package.stringifyConfig(cfg);
1312+
}
1313+
files[pxt.CONFIG_NAME] = pxt.Package.stringifyConfig(pulledConfig);
1314+
}
13061315
}
13071316

1308-
for (const fn of pxt.allPkgFiles(cfg).slice(1))
1317+
for (const fn of pxt.allPkgFiles(pulledConfig).slice(1))
13091318
await downloadAsync(fn)
13101319

1311-
if (!cfg.name) {
1312-
cfg.name = parsed.fileName && parsed.project
1320+
if (!pulledConfig.name) {
1321+
pulledConfig.name = parsed.fileName && parsed.project
13131322
// when creating nested project, mangle name
13141323
? `${parsed.project}-${parsed.fileName}`
13151324
: (parsed.project || parsed.fullName)
1316-
cfg.name = cfg.name.replace(/pxt-/ig, '')
1325+
pulledConfig.name = pulledConfig.name.replace(/pxt-/ig, '')
13171326
.replace(/\//g, '-')
13181327
.replace(/-+/, "-")
13191328
.replace(/[^\w\-]/g, "")
13201329
if (!justJSON)
1321-
files[pxt.CONFIG_NAME] = pxt.Package.stringifyConfig(cfg);
1330+
files[pxt.CONFIG_NAME] = pxt.Package.stringifyConfig(pulledConfig);
13221331
}
13231332

13241333
if (!justJSON) {
@@ -1340,12 +1349,12 @@ async function githubUpdateToAsync(hd: Header, options: UpdateOptions) {
13401349
* If the repo was last opened in this target, use that version number in the header;
13411350
* otherwise, use current target version to avoid mismatched updates.
13421351
*/
1343-
const targetVersionToUse = (cfg.targetVersions?.targetId === pxt.appTarget.id && cfg.targetVersions.target) ?
1344-
cfg.targetVersions.target : pxt.appTarget.versions.target;
1352+
const targetVersionToUse = (pulledConfig.targetVersions?.targetId === pxt.appTarget.id && pulledConfig.targetVersions.target) ?
1353+
pulledConfig.targetVersions.target : pxt.appTarget.versions.target;
13451354

13461355
if (!hd) {
13471356
hd = await installAsync({
1348-
name: cfg.name,
1357+
name: pulledConfig.name,
13491358
githubId: repo,
13501359
pubId: "",
13511360
pubCurrent: false,
@@ -1355,7 +1364,7 @@ async function githubUpdateToAsync(hd: Header, options: UpdateOptions) {
13551364
targetVersion: targetVersionToUse,
13561365
}, files)
13571366
} else {
1358-
hd.name = cfg.name
1367+
hd.name = pulledConfig.name
13591368
await forceSaveAsync(hd, files)
13601369
}
13611370

0 commit comments

Comments
 (0)