Skip to content

Commit cabb9ad

Browse files
Use separate cache directory for each project / target (#201)
1 parent 34b86be commit cabb9ad

File tree

6 files changed

+25
-12
lines changed

6 files changed

+25
-12
lines changed

src/definitions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,6 @@ async function spawnImplTool(
115115
crystalOutputChannel.appendLine(`[Implementations] (${folder.name}) $ ${cmd}`);
116116

117117
return JSON.parse(
118-
await execAsync(cmd, folder.uri.fsPath)
118+
await execAsync(cmd, folder.uri.fsPath, `crystal-${folder.name}-${path.basename(main)}`)
119119
);
120120
}

src/hover.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ async function spawnContextTool(
276276

277277
crystalOutputChannel.appendLine(`[Context] (${folder.name}) $ ${cmd}`);
278278

279-
return await execAsync(cmd, folder.uri.fsPath)
279+
return await execAsync(cmd, folder.uri.fsPath, `crystal-${folder.name}-${path.basename(main)}`)
280280
.then((response) => {
281281
findProblems(response, document.uri);
282282
return JSON.parse(response);

src/macro.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Position, TextDocument, commands, window, workspace } from 'vscode';
22
import { crystalOutputChannel, execAsync, findProblems, getCompilerPath, getCursorPath, getShardMainPath, getWorkspaceFolder, shellEscape } from './tools';
3+
import path = require('path');
34

45
export const macroOutputChannel = window.createOutputChannel("Crystal Macro", "markdown")
56

@@ -39,13 +40,13 @@ export async function spawnMacroExpandTool(document: TextDocument, position: Pos
3940
const cmd = `${shellEscape(compiler)} tool expand ${shellEscape(main)} --cursor ${shellEscape(cursor)} ${config.get<string>("flags")}`
4041

4142
crystalOutputChannel.appendLine(`[Macro Expansion] (${folder.name}) $ ` + cmd)
42-
return await execAsync(cmd, folder.uri.fsPath)
43+
return await execAsync(cmd, folder.uri.fsPath, `crystal-${folder.name}-${path.basename(main)}`)
4344
.then((response) => {
4445
return response;
4546
})
4647
.catch(async (err) => {
4748
const new_cmd = cmd + ' -f json'
48-
await execAsync(new_cmd, folder.uri.fsPath)
49+
await execAsync(new_cmd, folder.uri.fsPath, `crystal-${folder.name}-${path.basename(main)}`)
4950
.catch((err) => {
5051
findProblems(err.stderr, document.uri)
5152
crystalOutputChannel.appendLine(`[Macro Expansion] Error: ${err.message}`)

src/problems.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { TextDocument, workspace } from "vscode";
22
import { setStatusBar, compiler_mutex, crystalOutputChannel, diagnosticCollection, execAsync, findProblems, getCompilerPath, getShardMainPath, getWorkspaceFolder, shellEscape } from "./tools";
3+
import path = require("path");
34

45
export function registerProblems(): void {
56
workspace.onDidOpenTextDocument((e) => handleDocument(e))
@@ -42,7 +43,7 @@ export async function spawnProblemsTool(document: TextDocument, mainFile: string
4243
const main = mainFile || await getShardMainPath(document);
4344
if (!main) return;
4445

45-
const folder = getWorkspaceFolder(document.uri).uri.fsPath;
46+
const folder = getWorkspaceFolder(document.uri);
4647
const config = workspace.getConfiguration('crystal-lang');
4748

4849
// If document is in a folder of the same name as the document, it will throw an
@@ -56,7 +57,7 @@ export async function spawnProblemsTool(document: TextDocument, mainFile: string
5657
const cmd = `${shellEscape(compiler)} build ${shellEscape(main)} --no-debug --no-color --no-codegen --error-trace -f json -o ${output} ${config.get<string>("flags")}`
5758

5859
crystalOutputChannel.appendLine(`[Problems] (${getWorkspaceFolder(document.uri).name}) $ ` + cmd)
59-
await execAsync(cmd, folder)
60+
await execAsync(cmd, folder.uri.fsPath, `crystal-${folder.name}-${path.basename(main)}`)
6061
.then((response) => {
6162
diagnosticCollection.clear()
6263
crystalOutputChannel.appendLine("[Problems] No problems found.")

src/spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ export async function spawnSpecTool(
377377
}
378378
crystalOutputChannel.appendLine(`[Spec] (${space.name}) $ ` + cmd);
379379

380-
await execAsync(cmd, space.uri.fsPath).catch((err) => {
380+
await execAsync(cmd, space.uri.fsPath, `crystal-${path.basename(space.uri.fsPath)}-specs`).catch((err) => {
381381
if (err.stderr) {
382382
findProblems(err.stderr, space.uri)
383383
} else if (err.message) {

src/tools.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as yaml from 'yaml';
77
import { cwd } from 'process';
88
import { Mutex } from 'async-mutex';
99
import { spawnProblemsTool } from './problems';
10+
import { homedir } from 'os';
1011

1112
export const crystalOutputChannel = window.createOutputChannel("Crystal", "log")
1213

@@ -27,6 +28,7 @@ export const compiler_mutex: Mutex = new Mutex();
2728
function execWrapper(
2829
command: string,
2930
cwd: string,
31+
target: string | null,
3032
callback?: (
3133
error: (ExecException & { stdout: string; stderr: string }) | {},
3234
stdout: string,
@@ -40,6 +42,15 @@ function execWrapper(
4042
env['GC_DONT_GC'] = '1'
4143
}
4244

45+
// Don't want to interfere with the global cache
46+
if (target) {
47+
const cache_dir = env['XDG_CACHE_HOME'] ||
48+
(homedir() ? path.join(homedir(), '.cache') : undefined) ||
49+
path.join(cwd, '.crystal');
50+
51+
env['CRYSTAL_CACHE_DIR'] = path.join(cache_dir, target)
52+
}
53+
4354
const response = exec(command, { 'cwd': cwd, env }, (err, stdout, stderr) => {
4455
if (err) {
4556
callback({ ...err, stderr, stdout }, stdout, stderr);
@@ -83,7 +94,7 @@ export async function getCompilerPath(): Promise<string> {
8394
const command =
8495
(process.platform === 'win32' ? 'where' : 'which') + ' crystal';
8596

86-
return (await execAsync(command, process.cwd())).trim();
97+
return (await execAsync(command, process.cwd(), null)).trim();
8798
}
8899

89100
/**
@@ -103,7 +114,7 @@ export async function getShardsPath(): Promise<string> {
103114
const command =
104115
(process.platform === 'win32' ? 'where' : 'which') + ' shards';
105116

106-
return (await execAsync(command, process.cwd())).trim();
117+
return (await execAsync(command, process.cwd(), null)).trim();
107118
}
108119

109120
/**
@@ -266,7 +277,7 @@ export async function getShardMainPath(document: TextDocument): Promise<string>
266277
*/
267278
export async function getCrystalLibPath(): Promise<string> {
268279
const compiler = await getCompilerPath();
269-
const libpath = await execAsync(`${compiler} env CRYSTAL_PATH`, process.cwd());
280+
const libpath = await execAsync(`${compiler} env CRYSTAL_PATH`, process.cwd(), null);
270281

271282
return libpath.replace(/^lib[:;]|(?:\r)?\n/g, '');
272283
}
@@ -317,7 +328,7 @@ export interface SemVer {
317328
export async function getCrystalVersion(): Promise<SemVer> {
318329
const compiler = await getCompilerPath();
319330
const cmd = `${shellEscape(compiler)} --version`
320-
const response = await execAsync(cmd, cwd())
331+
const response = await execAsync(cmd, cwd(), null)
321332

322333
const match = response.match(/Crystal (\d+)\.(\d+)\.(\d+)/)
323334

@@ -501,7 +512,7 @@ export async function getShardTargetForFile(document: TextDocument): Promise<{ r
501512
crystalOutputChannel.appendLine(`[Dependencies] ${space.name} $ ${cmd}`)
502513
const targetDocument = await workspace.openTextDocument(Uri.parse(targetPath))
503514

504-
const result = await execAsync(cmd, space.uri.fsPath)
515+
const result = await execAsync(cmd, space.uri.fsPath, null)
505516
.then((resp) => {
506517
return { response: resp, error: undefined };
507518
})

0 commit comments

Comments
 (0)