Skip to content

Commit d2faacb

Browse files
authored
Fix Offline mode (#3)
* fix starting install when no inet connection
1 parent 60f0e87 commit d2faacb

File tree

5 files changed

+68
-32
lines changed

5 files changed

+68
-32
lines changed

assets/welcome/pioarduino-ini-example.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
```ini
22
; Common configuration
33
[env]
4-
platform = https://github.com/pioarduino/platform-espressif32/releases/download/51.03.04/platform-espressif32.zip
4+
platform = https://github.com/pioarduino/platform-espressif32/releases/download/stable/platform-espressif32.zip
55
framework = arduino
66
board = esp32doit-devkit-v1
77
monitor_speed = 921600

src/installer/manager.js

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
* the root directory of this source tree.
77
*/
88

9-
import * as pioNodeHelpers from 'pioarduino-node-helpers';
9+
// DON'T import pioNodeHelpers here - it causes immediate initialization!
10+
// import * as pioNodeHelpers from 'pioarduino-node-helpers';
1011

1112
import PIOHome from '../home';
1213
import { PIO_CORE_VERSION_SPEC } from '../constants';
@@ -21,28 +22,10 @@ export default class InstallationManager {
2122

2223
constructor(disableAutoUpdates = false) {
2324
const config = vscode.workspace.getConfiguration('platformio-ide');
24-
this.stages = [
25-
new pioNodeHelpers.installer.pioarduinoCoreStage(
26-
{
27-
getValue: (key) => extension.context.globalState.get(key),
28-
setValue: (key, value) => extension.context.globalState.update(key, value),
29-
},
30-
this.onDidStatusChange.bind(this),
31-
{
32-
pioCoreVersionSpec: PIO_CORE_VERSION_SPEC,
33-
useBuiltinPython: config.get('useBuiltinPython'),
34-
useBuiltinPIOCore: config.get('useBuiltinPIOCore'),
35-
useDevelopmentPIOCore: config.get('useDevelopmentPIOCore'),
36-
pythonPrompt: new PythonPrompt(),
37-
disableAutoUpdates: disableAutoUpdates,
38-
predownloadedPackageDir: path.join(
39-
extension.context.extensionPath,
40-
'assets',
41-
'predownloaded',
42-
),
43-
},
44-
),
45-
];
25+
this.config = config;
26+
this.disableAutoUpdates = disableAutoUpdates;
27+
// Create stages lazily - the node-helpers now handle offline checks internally
28+
this.stages = null;
4629
}
4730

4831
onDidStatusChange() {
@@ -68,7 +51,39 @@ export default class InstallationManager {
6851
return new Date().getTime() - parseInt(lockTime) <= this.LOCK_TIMEOUT;
6952
}
7053

54+
createStages() {
55+
if (this.stages === null) {
56+
// Lazy load pioNodeHelpers only when needed
57+
const pioNodeHelpers = require('pioarduino-node-helpers');
58+
this.stages = [
59+
new pioNodeHelpers.installer.pioarduinoCoreStage(
60+
{
61+
getValue: (key) => extension.context.globalState.get(key),
62+
setValue: (key, value) => extension.context.globalState.update(key, value),
63+
},
64+
this.onDidStatusChange.bind(this),
65+
{
66+
pioCoreVersionSpec: PIO_CORE_VERSION_SPEC,
67+
useBuiltinPython: this.config.get('useBuiltinPython'),
68+
useBuiltinPIOCore: this.config.get('useBuiltinPIOCore'),
69+
useDevelopmentPIOCore: this.config.get('useDevelopmentPIOCore'),
70+
pythonPrompt: new PythonPrompt(),
71+
disableAutoUpdates: this.disableAutoUpdates,
72+
predownloadedPackageDir: path.join(
73+
extension.context.extensionPath,
74+
'assets',
75+
'predownloaded',
76+
),
77+
},
78+
),
79+
];
80+
}
81+
}
82+
7183
async check() {
84+
// Create stages if needed
85+
this.createStages();
86+
7287
let result = true;
7388
for (const stage of this.stages) {
7489
try {
@@ -77,13 +92,16 @@ export default class InstallationManager {
7792
}
7893
} catch (err) {
7994
result = false;
80-
console.warn(err);
95+
console.warn('Installation stage check failed:', err.message || err);
8196
}
8297
}
8398
return result;
8499
}
85100

86101
async install(progress) {
102+
// Ensure stages are created
103+
this.createStages();
104+
87105
const stageIncrementTotal = 100 / this.stages.length;
88106
// shutdown all PIO Home servers which block python.exe on Windows
89107
await PIOHome.shutdownAllServers();
@@ -99,6 +117,15 @@ export default class InstallationManager {
99117
}
100118

101119
destroy() {
102-
return this.stages.map((stage) => stage.destroy());
120+
if (this.stages) {
121+
for (const stage of this.stages) {
122+
try {
123+
if (stage && typeof stage.destroy === 'function') {
124+
stage.destroy();
125+
}
126+
} catch (err) {}
127+
}
128+
}
129+
this.stages = null;
103130
}
104131
}

src/main.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import * as misc from './misc';
10-
import * as pioNodeHelpers from 'pioarduino-node-helpers';
10+
// import * as pioNodeHelpers from 'pioarduino-node-helpers'; // Lazy load this!
1111
import * as piodebug from 'platformio-vscode-debug';
1212
import * as utils from './utils';
1313

@@ -148,6 +148,7 @@ class PlatformIOVSCodeExtension {
148148
if (this.getConfiguration('customPyPiIndexUrl')) {
149149
extraVars['PIP_INDEX_URL'] = this.getConfiguration('customPyPiIndexUrl');
150150
}
151+
const pioNodeHelpers = require('pioarduino-node-helpers');
151152
pioNodeHelpers.proc.patchOSEnviron({
152153
caller: 'vscode',
153154
extraPath: this.getConfiguration('customPATH'),
@@ -175,8 +176,12 @@ class PlatformIOVSCodeExtension {
175176
});
176177
try {
177178
return !(await im.check());
178-
} catch (err) {}
179-
return true;
179+
} catch (err) {
180+
// In case of network errors or other issues, assume an existing installation is OK
181+
// and no installation is required
182+
console.warn('Installation check failed:', err);
183+
return false; // No installation required
184+
}
180185
},
181186
);
182187

@@ -236,6 +241,7 @@ class PlatformIOVSCodeExtension {
236241
}
237242

238243
async startPIOHome() {
244+
const pioNodeHelpers = require('pioarduino-node-helpers');
239245
if (
240246
this.getConfiguration('disablePIOHomeStartup') ||
241247
!pioNodeHelpers.home.showAtStartup('vscode')
@@ -282,6 +288,7 @@ class PlatformIOVSCodeExtension {
282288
) {
283289
return;
284290
}
291+
const pioNodeHelpers = require('pioarduino-node-helpers');
285292
const envDir = pioNodeHelpers.core.getEnvDir();
286293
if (!envDir || !fs.isDirectorySync(envDir)) {
287294
return;

src/terminal.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export default class PIOTerminal {
2424
env: envClone,
2525
});
2626
// Set Codepage to UTF-8, if Windows is used
27-
if (process.platform === "win32") {
27+
if (process.platform === 'win32') {
2828
terminal.sendText('chcp 65001');
2929
}
3030
return terminal;
@@ -34,7 +34,7 @@ export default class PIOTerminal {
3434
if (!this._instance || this._instance.exitStatus !== undefined) {
3535
this._instance = this.new();
3636
// Set Codepage to UTF-8, if Windows is used
37-
if (process.platform === "win32") {
37+
if (process.platform === 'win32') {
3838
this._instance.sendText('chcp 65001');
3939
}
4040
}

src/utils.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* the root directory of this source tree.
77
*/
88

9-
import * as pioNodeHelpers from 'pioarduino-node-helpers';
9+
// import * as pioNodeHelpers from 'pioarduino-node-helpers'; // Lazy load!
1010

1111
import os from 'os';
1212
import vscode from 'vscode';
@@ -36,6 +36,7 @@ export async function notifyError(title, err) {
3636
${description}
3737
\`\`\`
3838
`;
39+
const pioNodeHelpers = require('pioarduino-node-helpers');
3940
const reportUrl = pioNodeHelpers.misc.getErrorReportUrl(title, ghbody);
4041

4142
let action = 'Report a problem';
@@ -62,6 +63,7 @@ export function getIDEVersion() {
6263
}
6364

6465
export async function listCoreSerialPorts() {
66+
const pioNodeHelpers = require('pioarduino-node-helpers');
6567
const script = `
6668
import json
6769
from platformio.public import list_serial_ports

0 commit comments

Comments
 (0)