-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: crash on receiving an LSP message in "stream" mode
Don't write log messages to stdout in "stream" mode. stdout is reserved for communicating with the LSP client in the "stream" mode. Write all types of log messages to stderr. Fixes LSP server crashing on the first message it receives (or maybe LSP client closing it because it received an invalid response). Also: - the callback to socket.write() was always logging the "err" parameter even if there was undefined which caused spurious lines in the console. Only actually log that if "err" is truthy. - Avoid too many newlines at the end of the log message as that also causes spurious empty lines in the log and the console. - Gracefully handle the case when the response for "workspace/configuration" returns a null value. Those configuration options have sane defaults and don't need to be present. - The Logger._stream property was removed as it was unused. Resolves #1708
- Loading branch information
Showing
6 changed files
with
100 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
packages/graphql-language-service-server/src/__tests__/Logger-test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* Copyright (c) 2020 GraphQL Contributors | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
import { tmpdir } from 'os'; | ||
import { Logger } from '../Logger'; | ||
|
||
describe('Logger', () => { | ||
let mockedStdoutWrite: jest.SpyInstance = null; | ||
let mockedStderrWrite: jest.SpyInstance = null; | ||
|
||
beforeEach(async () => { | ||
mockedStdoutWrite = jest | ||
.spyOn(process.stdout, 'write') | ||
.mockImplementation(() => true); | ||
mockedStderrWrite = jest | ||
.spyOn(process.stderr, 'write') | ||
.mockImplementation(() => true); | ||
}); | ||
|
||
afterEach(() => { | ||
mockedStdoutWrite.mockReset(); | ||
mockedStderrWrite.mockReset(); | ||
}); | ||
|
||
it('logs to stdout', () => { | ||
const logger = new Logger(tmpdir()); | ||
logger.log('log test'); | ||
|
||
expect(mockedStdoutWrite.mock.calls.length).toBe(1); | ||
expect(mockedStdoutWrite.mock.calls[0][0]).toContain('log test'); | ||
expect(mockedStderrWrite.mock.calls.length).toBe(0); | ||
}); | ||
|
||
it('logs to stderr', () => { | ||
const logger = new Logger(tmpdir()); | ||
logger.error('error test'); | ||
|
||
expect(mockedStdoutWrite.mock.calls.length).toBe(0); | ||
expect(mockedStderrWrite.mock.calls.length).toBe(1); | ||
expect(mockedStderrWrite.mock.calls[0][0]).toContain('error test'); | ||
}); | ||
|
||
it('only writes to stderr with "stderrOnly" enabled', () => { | ||
const stderrOnly = true; | ||
const logger = new Logger(tmpdir(), stderrOnly); | ||
logger.info('info test'); | ||
logger.warn('warn test'); | ||
logger.log('log test'); | ||
logger.error('error test'); | ||
|
||
expect(mockedStdoutWrite.mock.calls.length).toBe(0); | ||
expect(mockedStderrWrite.mock.calls.length).toBe(4); | ||
expect(mockedStderrWrite.mock.calls[0][0]).toContain('info test'); | ||
expect(mockedStderrWrite.mock.calls[1][0]).toContain('warn test'); | ||
expect(mockedStderrWrite.mock.calls[2][0]).toContain('log test'); | ||
expect(mockedStderrWrite.mock.calls[3][0]).toContain('error test'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters