Skip to content

Commit 15878a6

Browse files
committed
Download files to a temporary location first
So we know when it finished successfully
1 parent 084bbbd commit 15878a6

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ This is the Visual Studio Code extension for the [Haskell programming language](
1717
## Requirements
1818

1919
- For standalone `.hs`/`.lhs` files, [ghc](https://www.haskell.org/ghc/) must be installed and on the PATH. The easiest way to install it is with [ghcup](https://www.haskell.org/ghcup/).
20-
- For Cabal based projects, [cabal-install](https://www.haskell.org/cabal/) must be installed and on the PATH. It can also be installed with [ghcup](https://www.haskell.org/ghcup/).
20+
- For Cabal based projects, both ghc and [cabal-install](https://www.haskell.org/cabal/) must be installed and on the PATH. It can also be installed with [ghcup](https://www.haskell.org/ghcup/).
2121
- For Stack based projects, [stack](http://haskellstack.org) must be installed and on the PATH.
2222

2323
## Supported GHC versions
@@ -79,13 +79,13 @@ There are a few placeholders which will be expanded:
7979

8080
Haskell Language Server can display Haddock documentation on hover and in code completion for your code if you have built your project with haddock enabled.
8181

82-
For stack projects, in your project directory run
82+
For Stack projects, in your project directory run
8383

8484
```bash
8585
$ stack haddock --keep-going
8686
```
8787

88-
For cabal projects, run
88+
For Cabal projects, run
8989

9090
```bash
9191
$ cabal build --haddock

src/hlsBinaries.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,6 @@ export async function downloadServer(
155155
const serverName = `haskell-language-server-${release.tag_name}-${process.platform}-${ghcVersion}${exeExtension}`;
156156
const binaryDest = path.join(context.globalStoragePath, serverName);
157157

158-
if (fs.existsSync(binaryDest)) {
159-
return binaryDest;
160-
}
161-
162158
const title = `Downloading haskell-language-server ${release.tag_name} for GHC ${ghcVersion}`;
163159
try {
164160
await downloadFile(title, binaryURL, binaryDest);

src/utils.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,19 @@ import { createGunzip } from 'zlib';
1515
export const userAgentHeader = { 'User-Agent': 'vscode-hie-server' };
1616

1717
export async function downloadFile(titleMsg: string, srcUrl: url.UrlWithStringQuery, dest: string): Promise<void> {
18+
// If it already is downloaded just use that
1819
if (fs.existsSync(dest)) {
1920
return;
2021
}
2122

23+
// Download it to a .tmp location first, then rename it!
24+
// This way if the download fails halfway through or something then we know
25+
// to delete it and try again
26+
const downloadDest = dest + '.download';
27+
if (fs.existsSync(downloadDest)) {
28+
fs.unlinkSync(downloadDest);
29+
}
30+
2231
const downloadHieTask = window.withProgress(
2332
{
2433
location: ProgressLocation.Notification,
@@ -34,7 +43,7 @@ export async function downloadFile(titleMsg: string, srcUrl: url.UrlWithStringQu
3443
};
3544
getWithRedirects(opts, (res) => {
3645
const totalSize = parseInt(res.headers['content-length'] || '1', 10);
37-
const fileStream = fs.createWriteStream(dest, { mode: 0o744 });
46+
const fileStream = fs.createWriteStream(downloadDest, { mode: 0o744 });
3847
let curSize = 0;
3948

4049
// Decompress it if it's a gzip
@@ -58,14 +67,16 @@ export async function downloadFile(titleMsg: string, srcUrl: url.UrlWithStringQu
5867
fileStream.on('close', resolve);
5968
}).on('error', reject);
6069
});
61-
return p;
70+
await p;
71+
// Finally rename it to the actual dest
72+
fs.renameSync(downloadDest, dest);
6273
}
6374
);
6475

6576
try {
6677
return downloadHieTask;
6778
} catch (e) {
68-
fs.unlinkSync(dest);
79+
fs.unlinkSync(downloadDest);
6980
throw new Error(`Failed to download ${url.format(srcUrl)}`);
7081
}
7182
}

0 commit comments

Comments
 (0)