Repository: parcel-bundler/parcel
File: packages/utils/parcel-lsp/src/LspServer.ts (TypeScript)

Code before:

  client.onNotification(NotificationWorkspaceDiagnostics, diagnostics => {
    // console.log('got NotificationWorkspaceDiagnostics', diagnostics);
    for (let d of diagnostics) {
      uris.add(d.uri);
      connection.sendDiagnostics(d);
    }
  });

  client.onClose(() => {
    clients.delete(JSON.stringify(metafile));
    sendDiagnosticsRefresh();
    return Promise.all(
      [...uris].map(uri => connection.sendDiagnostics({uri, diagnostics: []})),
    );
  });

  sendDiagnosticsRefresh();
  clients.set(JSON.stringify(metafile), result);
}

// Take realpath because to have consistent cache keys on macOS (/var -> /private/var)
const BASEDIR = path.join(fs.realpathSync(os.tmpdir()), 'parcel-lsp');
fs.mkdirSync(BASEDIR, {recursive: true});

fs.writeFileSync(path.join(BASEDIR, `lsp-server`), 'help');

// Search for currently running Parcel processes in the parcel-lsp dir.
// Create an IPC client connection for each running process.
for (let filename of fs.readdirSync(BASEDIR)) {
  if (!filename.endsWith('.json')) continue;
  let filepath = path.join(BASEDIR, filename);
  const contents = parseMetafile(filepath);
  const {projectRoot} = contents;

  if (WORKSPACE_ROOT === projectRoot) {
    createClient(filepath, contents);
  }
  console.log('connected initial', filepath);
}

// Watch for new Parcel processes in the parcel-lsp dir, and disconnect the
// client for each corresponding connection when a Parcel process ends
watcher.subscribe(BASEDIR, async (err, events) => {
  if (err) {
    throw err;
  }

  for (let event of events) {
    if (event.type === 'create' && event.path.endsWith('.json')) {
      const file = fs.readFileSync(event.path, 'utf-8');

Review comment (suggestion):
```suggestion
fs.writeFileSync(path.join(BASEDIR, LSP_SENTINEL_FILENAME), '');
```

Code after:

  client.onNotification(NotificationWorkspaceDiagnostics, diagnostics => {
    // console.log('got NotificationWorkspaceDiagnostics', diagnostics);
    for (let d of diagnostics) {
      uris.add(d.uri);
      connection.sendDiagnostics(d);
    }
  });

  client.onClose(() => {
    clients.delete(JSON.stringify(metafile));
    sendDiagnosticsRefresh();
    return Promise.all(
      [...uris].map(uri => connection.sendDiagnostics({uri, diagnostics: []})),
    );
  });

  sendDiagnosticsRefresh();
  clients.set(JSON.stringify(metafile), result);
}

// Take realpath because to have consistent cache keys on macOS (/var -> /private/var)
const BASEDIR = path.join(fs.realpathSync(os.tmpdir()), 'parcel-lsp');
fs.mkdirSync(BASEDIR, {recursive: true});

fs.writeFileSync(path.join(BASEDIR, LSP_SENTINEL_FILENAME), '');

// Search for currently running Parcel processes in the parcel-lsp dir.
// Create an IPC client connection for each running process.
for (let filename of fs.readdirSync(BASEDIR)) {
  if (!filename.endsWith('.json')) continue;
  let filepath = path.join(BASEDIR, filename);
  const contents = parseMetafile(filepath);
  const {projectRoot} = contents;

  if (WORKSPACE_ROOT === projectRoot) {
    createClient(filepath, contents);
  }
}

// Watch for new Parcel processes in the parcel-lsp dir, and disconnect the
// client for each corresponding connection when a Parcel process ends
watcher.subscribe(BASEDIR, async (err, events) => {
  if (err) {
    throw err;
  }

  for (let event of events) {
    if (event.type === 'create' && event.path.endsWith('.json')) {
      const file = fs.readFileSync(event.path, 'utf-8');
      const contents = parseMetafile(file);