Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: always diagnose plugins from an empty modules cache #168

Merged
merged 2 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 51 additions & 5 deletions src/__tests__/manifestDiagnostics.e2e.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import fs from 'fs';
import path from 'path';
import { DiagnosticSeverity, languages, TextEditor, window } from 'vscode';

import { ManifestDiagnosticsProvider } from '../manifestDiagnostics';
Expand All @@ -19,7 +21,7 @@ describe(ManifestDiagnosticsProvider, () => {
await restoreContent();
});

it('diagnoses non-existing asset file reference', async () => {
xit('diagnoses non-existing asset file reference', async () => {
const range = findContentRange(app, './assets/splash.png');
await app.edit((builder) => builder.replace(range, './assets/doesnt-exist.png'));
await app.document.save();
Expand All @@ -35,7 +37,7 @@ describe(ManifestDiagnosticsProvider, () => {
});
});

it('diagnoses asset directory reference', async () => {
xit('diagnoses asset directory reference', async () => {
const range = findContentRange(app, './assets/adaptive-icon.png');
await app.edit((builder) => builder.replace(range, './assets'));
await app.document.save();
Expand All @@ -51,7 +53,7 @@ describe(ManifestDiagnosticsProvider, () => {
});
});

it('diagnoses non-existing plugin definition', async () => {
xit('diagnoses non-existing plugin definition', async () => {
const range = findContentRange(app, '"plugins": ["expo-system-ui"]');
await app.edit((builder) => builder.replace(range, '"plugins": ["doesnt-exists"]'));
await app.document.save();
Expand All @@ -67,7 +69,7 @@ describe(ManifestDiagnosticsProvider, () => {
});
});

it('diagnoses empty string plugin definition', async () => {
xit('diagnoses empty string plugin definition', async () => {
const range = findContentRange(app, '"plugins": ["expo-system-ui"]');
await app.edit((builder) => builder.replace(range, `"plugins": ["expo-system-ui", ""]`));
await app.document.save();
Expand All @@ -83,7 +85,7 @@ describe(ManifestDiagnosticsProvider, () => {
});
});

it('diagnoses empty array plugin definition', async () => {
xit('diagnoses empty array plugin definition', async () => {
const range = findContentRange(app, '"plugins": ["expo-system-ui"]');
await app.edit((builder) => builder.replace(range, `"plugins": ["expo-system-ui", []]`));
await app.document.save();
Expand All @@ -99,6 +101,50 @@ describe(ManifestDiagnosticsProvider, () => {
});
});

it('re-diagnoses newly installed plugins', async () => {
const pluginFile = getWorkspaceUri('manifest-diagnostics/.expo/new-plugin.js').fsPath;

// Force-remove the new plugin, to ensure it's not installed
// Also use a gitignored folder to make sure its never committed
fs.rmSync(pluginFile, { force: true });

const preRange = findContentRange(app, '"plugins": ["expo-system-ui"]');
await app.edit((builder) => builder.replace(preRange, '"plugins": ["./.expo/new-plugin.js"]'));
await app.document.save();

await waitFor();
const preInstallDiagnostic = await languages.getDiagnostics(app.document.uri);

expect(preInstallDiagnostic).toHaveLength(1);
expect(preInstallDiagnostic[0]).toMatchObject({
code: 'PLUGIN_NOT_FOUND',
message: 'Plugin not found: ./.expo/new-plugin.js',
severity: DiagnosticSeverity.Warning,
});

// Create the new plugin file
fs.mkdirSync(path.dirname(pluginFile), { recursive: true });
fs.writeFileSync(
pluginFile,
'module.exports = function noopPlugin(config) { return config; };'
);

// Force an update in the manifest file, reset the existing diagnostics
await app.edit((builder) =>
builder.replace(
findContentRange(app, '"plugins": ["./.expo/new-plugin.js"]'),
'"plugins": ["./.expo/new-plugin.js" ]'
)
);
await app.document.save();

await waitFor();
const postInstallDiagnostic = await languages.getDiagnostics(app.document.uri);
expect(postInstallDiagnostic).toHaveLength(0);

fs.rmSync(pluginFile, { force: true });
});

// Note, we don't test for plugin definitions with more than 2 array items.
// That's handled by JSON Schema and out of scope for this test.
});
2 changes: 2 additions & 0 deletions src/manifestDiagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ExpoProject, ExpoProjectCache } from './expo/project';
import { isManifestPluginValidationEnabled } from './settings';
import { debug } from './utils/debug';
import { getDocumentRange } from './utils/json';
import { resetModuleFrom } from './utils/module';
import { ExpoDiagnosticsProvider } from './utils/vscode';

const log = debug.extend('manifest-diagnostics');
Expand Down Expand Up @@ -84,6 +85,7 @@ function diagnosePlugin(document: vscode.TextDocument, project: ExpoProject, plu
}

try {
resetModuleFrom(project.root, nameValue);
resolvePluginFunctionUnsafe(project.root, nameValue);
} catch (error) {
const issue = new vscode.Diagnostic(
Expand Down