Skip to content

Commit f3dc7b5

Browse files
committed
sagas/mpy: Use legacy __main__ on old firmware.
Needed for backwards compatibility. Older firmware always look for __main__. See pybricks/support#2364
1 parent a18d88d commit f3dc7b5

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

src/hub/reducers.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ test('initial state', () => {
5858
"runtime": "hub.runtime.disconnected",
5959
"selectedSlot": 0,
6060
"useLegacyDownload": false,
61+
"useLegacyMainModule": false,
6162
"useLegacyStartUserProgram": false,
6263
"useLegacyStdio": false,
6364
}
@@ -544,3 +545,23 @@ describe('useLegacyStartUserProgram', () => {
544545
).toBeFalsy();
545546
});
546547
});
548+
549+
describe('useLegacyMainModule', () => {
550+
test('Pybricks Profile < v1.5.0', () => {
551+
expect(
552+
reducers(
553+
{ useLegacyMainModule: false } as State,
554+
bleDIServiceDidReceiveSoftwareRevision('1.4.0'),
555+
).useLegacyMainModule,
556+
).toBeTruthy();
557+
});
558+
559+
test('Pybricks Profile >= v1.5.0', () => {
560+
expect(
561+
reducers(
562+
{ useLegacyMainModule: true } as State,
563+
bleDIServiceDidReceiveSoftwareRevision('1.5.0'),
564+
).useLegacyMainModule,
565+
).toBeFalsy();
566+
});
567+
});

src/hub/reducers.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,18 @@ const useLegacyStartUserProgram: Reducer<boolean> = (state = false, action) => {
381381
return state;
382382
};
383383

384+
/**
385+
* When true, use the legacy `__main__` module name instead of the actual file name.
386+
*/
387+
const useLegacyMainModule: Reducer<boolean> = (state = false, action) => {
388+
if (bleDIServiceDidReceiveSoftwareRevision.matches(action)) {
389+
// Behavior changed starting with Pybricks Profile v1.5.0.
390+
return !semver.satisfies(action.version, '^1.5.0');
391+
}
392+
393+
return state;
394+
};
395+
384396
/*
385397
* Returns number of available slots or 0 for slots not supported.
386398
*/
@@ -418,6 +430,7 @@ export default combineReducers({
418430
useLegacyDownload,
419431
useLegacyStdio,
420432
useLegacyStartUserProgram,
433+
useLegacyMainModule,
421434
numOfSlots,
422435
selectedSlot,
423436
});

src/mpy/sagas.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ function* handleCompile(action: ReturnType<typeof compile>): Generator {
104104
/**
105105
* Compiles code into the Pybricks multi-mpy6 file format.
106106
*
107-
* This includes a __main__ module which is the file currently open in the
108-
* editor and any imported modules that can be found in the user file system.
107+
* This includes the file currently open in the editor and any imported modules
108+
* that can be found in the user file system.
109109
*/
110110
function* handleCompileMulti6(): Generator {
111111
// REVISIT: should we be getting the active file here or have it as an
@@ -128,16 +128,22 @@ function* handleCompileMulti6(): Generator {
128128
return;
129129
}
130130

131-
const mainPy = yield* editorGetValue();
131+
const useLegacyMainModule = yield* select(
132+
(s: RootState) => s.hub.useLegacyMainModule,
133+
);
134+
135+
const mainPyContents = yield* editorGetValue();
132136
const mainPyPath = metadata.path ?? '__main__.py';
133-
const mainPyName = mainPyPath.replace(/\.[^.]+$/, '');
137+
const mainPyName = useLegacyMainModule
138+
? '__main__'
139+
: mainPyPath.replace(/\.[^.]+$/, '');
134140

135141
const pyFiles = new Map<string, FileContents>([
136-
[mainPyName, { path: mainPyPath, contents: mainPy }],
142+
[mainPyName, { path: mainPyPath, contents: mainPyContents }],
137143
]);
138144

139145
const checkedModules = new Set<string>([mainPyName]);
140-
const uncheckedScripts = new Array<string>(mainPy);
146+
const uncheckedScripts = new Array<string>(mainPyContents);
141147

142148
for (;;) {
143149
// parse all unchecked scripts to find imported modules that haven't

0 commit comments

Comments
 (0)