Skip to content

Commit 88ce9ab

Browse files
feat: copy latest server log file (#36)
1 parent c4310c5 commit 88ce9ab

File tree

9 files changed

+148
-20
lines changed

9 files changed

+148
-20
lines changed

.vscode/launch.json

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,6 @@
7272
"outFiles": ["${workspaceFolder}/out/**/*.js"],
7373
"sourceMapRenames": true
7474
},
75-
{
76-
"name": "🧩 Debug Extension (Workspace With Settings)",
77-
"type": "extensionHost",
78-
"request": "launch",
79-
"args": [
80-
"--disable-extensions",
81-
"--extensionDevelopmentKind=node",
82-
"--extensionDevelopmentPath=${workspaceFolder}",
83-
"--log=supabase.postgrestools:debug",
84-
"${workspaceFolder}/test/fixtures/workspace-with-settings"
85-
],
86-
"outFiles": ["${workspaceFolder}/out/**/*.js"],
87-
"sourceMapRenames": true
88-
},
8975
{
9076
"name": "🧩 Debug Extension (Yarn PnP Strategy)",
9177
"type": "extensionHost",

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,20 @@ You should find your remote database settings at `https://supabase.com/dashboard
5151

5252
## Useful Commands
5353

54-
The extension adds six commands to your VS Code Command Palette. They are all prefixed by `PostgresTools`.
54+
The extension adds seven commands to your VS Code Command Palette. They are all prefixed by `PostgresTools`.
5555

5656
- `PostgresTools: Hard Reset (Delete All Temp and Global Binaries)` is your troubleshooting weapon. It will basically remove all binaries that were copied and downloaded _by the extension_ (not those you have installed or copied yourself). The extension will then again search for a server binary via the strategies mentioned in [the setup](#setting-up-the-lsp-server).
5757
- `PostgresTools: Download Server` lets you select and download the server binary. It'll be the matching version for your machine. If you set `postgrestools.allowDownloadPrereleases` to true in yor VS Code settings, you'll be able to select prereleases.
58-
- `PostgresTools: Get Current Version` will print the current version if the LSP server is running.
58+
- `PostgresTools: Get Current Version` will print the current version and the strategy with which the server binary was found.
5959
- `PostgresTools: Start` and `PostgresTools: Stop` will stop or start the LSP server and the client.
6060
- `PostgresTools: Restart` runs stop and start in succession.
61+
- `PostgresTools: Copy Latest Server Logfile` copies the latest server log file to your currently opened repo. The log file is meant to be attached to GitHub issues, it can sometimes help us to debug.
6162

6263
## Troubleshooting
6364

6465
1. First, try restarting the extension via the `PostgresTools: Hard Reset (...)` command mentioned above.
6566
2. Open your VS Code Terminal, select the tab `Output`, and select one of the `postgrestools` extensions on the right. You might see what went wrong in the logs.
67+
3. If you want to open a GitHub issue, it can sometimes help us if you attach the LSP server log file. We provide the `PostgresTools: Copy Latest Server Logfile` command to make that as easy as possible.
6668

6769
## FAQ
6870

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "postgrestools",
33
"publisher": "Supabase",
44
"description": "Postgres Language Server right in your IDE.",
5-
"version": "1.2.0",
5+
"version": "1.2.1",
66
"repository": {
77
"type": "git",
88
"url": "https://github.com/supabase-community/postgrestools-vscode"
@@ -54,6 +54,10 @@
5454
{
5555
"title": "PostgresTools: Get Current Version",
5656
"command": "postgrestools.currentVersion"
57+
},
58+
{
59+
"title": "PostgresTools: Copy Latest Server Logfile",
60+
"command": "postgrestools.copyLatestLogfile"
5761
}
5862
],
5963
"configuration": {

src/commands.ts

Lines changed: 131 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
import { window } from "vscode";
1+
import { Uri, window, workspace } from "vscode";
22
import { downloadPglt, getDownloadedBinary } from "./downloader";
33
import { restart, start, stop } from "./lifecycle";
44
import { logger } from "./logger";
55
import { state } from "./state";
66
import {
77
clearGlobalBinaries,
88
clearTemporaryBinaries,
9+
dirExists,
910
getVersion,
1011
} from "./utils";
1112
import { Releases } from "./releases";
13+
import { join } from "path";
14+
import os from "node:os";
1215

1316
/**
1417
* These commands are exposed to the user via the Command Palette.
@@ -75,4 +78,131 @@ export class UserFacingCommands {
7578
);
7679
}
7780
}
81+
82+
static async copyLatestLogfile() {
83+
logger.info("Starting to copy latest log file…");
84+
85+
let logdir;
86+
87+
if (process.env.PGT_LOG_PATH) {
88+
logdir = Uri.file(process.env.PGT_LOG_PATH);
89+
} else {
90+
/*
91+
* Looks are placed at different locations based on the platform.
92+
* Linux: /home/alice/.cache/pgt
93+
* Win: C:\Users\Alice\AppData\Local\supabase-community\pgt\cache
94+
* Mac: /Users/Alice/Library/Caches/dev.supabase-community.pgt
95+
*/
96+
switch (process.platform) {
97+
case "darwin": {
98+
logdir = Uri.file(
99+
join(
100+
os.homedir(),
101+
"Library",
102+
"Caches",
103+
"dev.supabase-community.pgt"
104+
)
105+
);
106+
break;
107+
}
108+
case "linux": {
109+
logdir = Uri.file(join(os.homedir(), ".cache", "pgt"));
110+
break;
111+
}
112+
case "win32": {
113+
logdir = Uri.file(
114+
join(
115+
os.homedir(),
116+
"AppData",
117+
"Local",
118+
"supabase-community",
119+
"pgt",
120+
"cache"
121+
)
122+
);
123+
break;
124+
}
125+
default: {
126+
window.showErrorMessage(
127+
`Unsupported Platform: ${process.platform}. PostgresTools only runs on linux, darwin and windows.`
128+
);
129+
return;
130+
}
131+
}
132+
}
133+
134+
logger.info("Determined log directory location", {
135+
logdir: logdir.fsPath,
136+
});
137+
138+
if (!(await dirExists(logdir))) {
139+
window.showErrorMessage(`Did not find expected log directory.`);
140+
return;
141+
}
142+
143+
const logFiles = await workspace.fs
144+
.readDirectory(logdir)
145+
.then((files) => files.filter(([name]) => name.startsWith("server.log")));
146+
147+
if (logFiles.length === 0) {
148+
window.showErrorMessage(`No log files found in log directory.`);
149+
return;
150+
}
151+
152+
logger.info(`Found ${logFiles.length} log files.`);
153+
154+
if (!state.activeProject) {
155+
window.showErrorMessage(
156+
`No active project, can't determine a location to copy the log file.`
157+
);
158+
return;
159+
}
160+
161+
logFiles.sort((a, b) => {
162+
const [dateA, idxA] = getDatePartsFromLogFile(a[0]);
163+
const [dateB, idxB] = getDatePartsFromLogFile(b[0]);
164+
165+
if (dateA.getTime() === dateB.getTime()) {
166+
return idxB - idxA;
167+
} else {
168+
return dateB.getTime() - dateA.getTime();
169+
}
170+
});
171+
172+
const [latestFilename] = logFiles[0];
173+
174+
logger.info(`Identified latest log file.`, {
175+
filename: latestFilename,
176+
});
177+
178+
try {
179+
const target = Uri.joinPath(state.activeProject.path, "server.log");
180+
181+
await workspace.fs.copy(Uri.joinPath(logdir, latestFilename), target);
182+
183+
window.showInformationMessage(`Copied log file to ${target.fsPath}.`);
184+
} catch (err) {
185+
logger.error(`Error copying log file: ${err}`);
186+
window.showErrorMessage(
187+
`Error copying log file. View Output for more information.`
188+
);
189+
}
190+
}
191+
}
192+
193+
/** Expected format is server.log.2025-01-01-17 */
194+
function getDatePartsFromLogFile(filename: string): [Date, number] {
195+
try {
196+
const last = filename.split(".").pop()!;
197+
const [year, month, day, idx] = last.split("-");
198+
199+
if (!year || !month || !day || !idx || Number.isNaN(+idx)) {
200+
throw new Error();
201+
}
202+
203+
return [new Date(`${year}-${month}-${day}`), +idx];
204+
} catch {
205+
logger.warn(`Unexpected log file name: ${filename}`);
206+
return [new Date(0), 999];
207+
}
78208
}

src/extension.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ const registerUserFacingCommands = () => {
4747
commands.registerCommand(
4848
"postgrestools.currentVersion",
4949
UserFacingCommands.currentVersion
50+
),
51+
commands.registerCommand(
52+
"postgrestools.copyLatestLogfile",
53+
UserFacingCommands.copyLatestLogfile
5054
)
5155
);
5256

src/session.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ export const createActiveSession = async () => {
218218
return;
219219
}
220220

221+
state.activeProject = activeProject;
221222
state.activeSession = await createSession(activeProject);
222223

223224
try {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"postgrestools.configFile": "./configs/postgrestools.jsonc",
23
"postgrestools.bin": "./bin/postgrestools"
34
// "postgrestools.bin": "/bin/postgrestools"
45
}

0 commit comments

Comments
 (0)