2021-09-30 07:29:25 -04:00
|
|
|
import getMeta from '../../../utils/meta'
|
|
|
|
import HumanReadableLogs from '../../../ide/human-readable-logs/HumanReadableLogs'
|
|
|
|
import BibLogParser from '../../../ide/log-parser/bib-log-parser'
|
2021-12-07 04:18:48 -05:00
|
|
|
import { v4 as uuid } from 'uuid'
|
2022-07-20 04:32:05 -04:00
|
|
|
import { enablePdfCaching } from './pdf-caching-flags'
|
2023-09-27 05:45:49 -04:00
|
|
|
import { debugConsole } from '@/utils/debugging'
|
2023-10-26 06:01:08 -04:00
|
|
|
import { dirname, findEntityByPath } from '@/features/file-tree/util/path'
|
2021-09-30 07:29:25 -04:00
|
|
|
|
2022-07-04 08:25:24 -04:00
|
|
|
// Warnings that may disappear after a second LaTeX pass
|
|
|
|
const TRANSIENT_WARNING_REGEX = /^(Reference|Citation).+undefined on input line/
|
2021-09-30 07:29:25 -04:00
|
|
|
|
2022-05-18 06:37:25 -04:00
|
|
|
export function handleOutputFiles(outputFiles, projectId, data) {
|
2021-09-30 07:29:25 -04:00
|
|
|
const outputFile = outputFiles.get('output.pdf')
|
2022-07-13 08:56:30 -04:00
|
|
|
if (!outputFile) return null
|
2021-09-30 07:29:25 -04:00
|
|
|
|
2022-07-13 08:56:30 -04:00
|
|
|
// build the URL for viewing the PDF in the preview UI
|
|
|
|
const params = new URLSearchParams({
|
|
|
|
compileGroup: data.compileGroup,
|
|
|
|
})
|
2021-09-30 07:29:25 -04:00
|
|
|
|
2022-07-13 08:56:30 -04:00
|
|
|
if (data.clsiServerId) {
|
|
|
|
params.set('clsiserverid', data.clsiServerId)
|
|
|
|
}
|
2021-09-30 07:29:25 -04:00
|
|
|
|
2022-07-20 04:32:05 -04:00
|
|
|
if (enablePdfCaching) {
|
2022-07-13 08:56:30 -04:00
|
|
|
// Tag traffic that uses the pdf caching logic.
|
|
|
|
params.set('enable_pdf_caching', 'true')
|
|
|
|
}
|
2021-09-30 07:29:25 -04:00
|
|
|
|
2022-07-13 08:56:30 -04:00
|
|
|
outputFile.pdfUrl = `${buildURL(
|
|
|
|
outputFile,
|
2023-06-27 09:37:53 -04:00
|
|
|
data.pdfDownloadDomain
|
2022-07-13 08:56:30 -04:00
|
|
|
)}?${params}`
|
2021-09-30 07:29:25 -04:00
|
|
|
|
2022-07-13 08:56:30 -04:00
|
|
|
// build the URL for downloading the PDF
|
|
|
|
params.set('popupDownload', 'true') // save PDF download as file
|
2021-09-30 07:29:25 -04:00
|
|
|
|
2022-07-13 08:56:30 -04:00
|
|
|
outputFile.pdfDownloadUrl = `/download/project/${projectId}/build/${outputFile.build}/output/output.pdf?${params}`
|
2021-09-30 07:29:25 -04:00
|
|
|
|
2022-07-13 08:56:30 -04:00
|
|
|
return outputFile
|
2022-03-25 05:42:10 -04:00
|
|
|
}
|
2021-09-30 07:29:25 -04:00
|
|
|
|
2022-03-25 05:42:10 -04:00
|
|
|
export const handleLogFiles = async (outputFiles, data, signal) => {
|
|
|
|
const result = {
|
|
|
|
log: null,
|
|
|
|
logEntries: {
|
|
|
|
errors: [],
|
|
|
|
warnings: [],
|
|
|
|
typesetting: [],
|
|
|
|
},
|
2021-09-30 07:29:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function accumulateResults(newEntries, type) {
|
|
|
|
for (const key in result.logEntries) {
|
|
|
|
if (newEntries[key]) {
|
|
|
|
for (const entry of newEntries[key]) {
|
|
|
|
if (type) {
|
|
|
|
entry.type = newEntries.type
|
|
|
|
}
|
|
|
|
if (entry.file) {
|
|
|
|
entry.file = normalizeFilePath(entry.file)
|
|
|
|
}
|
2021-12-07 04:18:48 -05:00
|
|
|
entry.key = uuid()
|
2021-09-30 07:29:25 -04:00
|
|
|
}
|
|
|
|
result.logEntries[key].push(...newEntries[key])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const logFile = outputFiles.get('output.log')
|
|
|
|
|
|
|
|
if (logFile) {
|
2022-03-25 05:42:10 -04:00
|
|
|
try {
|
2023-06-27 09:37:53 -04:00
|
|
|
const response = await fetch(buildURL(logFile, data.pdfDownloadDomain), {
|
|
|
|
signal,
|
|
|
|
})
|
2021-09-30 07:29:25 -04:00
|
|
|
|
2022-03-25 05:42:10 -04:00
|
|
|
result.log = await response.text()
|
2021-09-30 07:29:25 -04:00
|
|
|
|
2022-07-04 08:25:24 -04:00
|
|
|
let { errors, warnings, typesetting } = HumanReadableLogs.parse(
|
2022-03-25 05:42:10 -04:00
|
|
|
result.log,
|
|
|
|
{
|
|
|
|
ignoreDuplicates: true,
|
2022-06-14 06:08:32 -04:00
|
|
|
oldRegexes:
|
2022-06-14 11:51:40 -04:00
|
|
|
getMeta('ol-splitTestVariants')?.['latex-log-parser'] !== 'new',
|
2022-03-25 05:42:10 -04:00
|
|
|
}
|
|
|
|
)
|
2021-09-30 07:29:25 -04:00
|
|
|
|
2022-07-04 08:25:24 -04:00
|
|
|
if (data.status === 'stopped-on-first-error') {
|
|
|
|
// Hide warnings that could disappear after a second pass
|
|
|
|
warnings = warnings.filter(warning => !isTransientWarning(warning))
|
|
|
|
}
|
|
|
|
|
2022-03-25 05:42:10 -04:00
|
|
|
accumulateResults({ errors, warnings, typesetting })
|
|
|
|
} catch (e) {
|
2023-09-27 05:45:49 -04:00
|
|
|
debugConsole.warn(e) // ignore failure to fetch/parse the log file, but log a warning
|
2022-03-25 05:42:10 -04:00
|
|
|
}
|
2021-09-30 07:29:25 -04:00
|
|
|
}
|
|
|
|
|
2023-06-15 09:21:35 -04:00
|
|
|
const blgFiles = []
|
2021-09-30 07:29:25 -04:00
|
|
|
|
2023-06-15 09:21:35 -04:00
|
|
|
for (const [filename, file] of outputFiles) {
|
|
|
|
if (filename.endsWith('.blg')) {
|
|
|
|
blgFiles.push(file)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const blgFile of blgFiles) {
|
2021-11-25 05:23:16 -05:00
|
|
|
try {
|
2023-06-27 09:37:53 -04:00
|
|
|
const response = await fetch(buildURL(blgFile, data.pdfDownloadDomain), {
|
|
|
|
signal,
|
|
|
|
})
|
2022-03-25 05:42:10 -04:00
|
|
|
|
|
|
|
const log = await response.text()
|
|
|
|
|
|
|
|
try {
|
|
|
|
const { errors, warnings } = new BibLogParser(log, {
|
|
|
|
maxErrors: 100,
|
|
|
|
}).parse()
|
|
|
|
accumulateResults({ errors, warnings }, 'BibTeX:')
|
|
|
|
} catch (e) {
|
|
|
|
// BibLog parsing errors are ignored
|
|
|
|
}
|
2021-11-25 05:23:16 -05:00
|
|
|
} catch (e) {
|
2023-09-27 05:45:49 -04:00
|
|
|
debugConsole.warn(e) // ignore failure to fetch/parse the log file, but log a warning
|
2021-11-25 05:23:16 -05:00
|
|
|
}
|
2021-09-30 07:29:25 -04:00
|
|
|
}
|
|
|
|
|
2021-11-25 05:24:25 -05:00
|
|
|
result.logEntries.all = [
|
|
|
|
...result.logEntries.errors,
|
|
|
|
...result.logEntries.warnings,
|
|
|
|
...result.logEntries.typesetting,
|
|
|
|
]
|
|
|
|
|
2021-09-30 07:29:25 -04:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2023-10-26 06:01:08 -04:00
|
|
|
export function buildLogEntryAnnotations(entries, fileTreeData, rootDocId) {
|
|
|
|
const rootDocDirname = dirname(fileTreeData, rootDocId)
|
2021-09-30 07:29:25 -04:00
|
|
|
|
|
|
|
const logEntryAnnotations = {}
|
|
|
|
|
|
|
|
for (const entry of entries) {
|
|
|
|
if (entry.file) {
|
|
|
|
entry.file = normalizeFilePath(entry.file, rootDocDirname)
|
|
|
|
|
2023-10-26 06:01:08 -04:00
|
|
|
const entity = findEntityByPath(fileTreeData, entry.file)?.entity
|
2021-09-30 07:29:25 -04:00
|
|
|
|
|
|
|
if (entity) {
|
2023-10-26 06:01:08 -04:00
|
|
|
if (!(entity._id in logEntryAnnotations)) {
|
|
|
|
logEntryAnnotations[entity._id] = []
|
2021-09-30 07:29:25 -04:00
|
|
|
}
|
|
|
|
|
2023-10-26 06:01:08 -04:00
|
|
|
logEntryAnnotations[entity._id].push({
|
2021-09-30 07:29:25 -04:00
|
|
|
row: entry.line - 1,
|
|
|
|
type: entry.level === 'error' ? 'error' : 'warning',
|
|
|
|
text: entry.message,
|
2022-06-21 05:58:56 -04:00
|
|
|
source: 'compile', // NOTE: this is used in Ace for filtering the annotations
|
2024-01-23 08:00:32 -05:00
|
|
|
ruleId: entry.ruleId,
|
2021-09-30 07:29:25 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return logEntryAnnotations
|
|
|
|
}
|
|
|
|
|
2024-02-05 06:41:49 -05:00
|
|
|
export const buildRuleCounts = (entries = []) => {
|
|
|
|
const counts = {}
|
|
|
|
for (const entry of entries) {
|
|
|
|
const key = `${entry.level}_${entry.ruleId}`
|
|
|
|
counts[key] = counts[key] ? counts[key] + 1 : 1
|
|
|
|
}
|
|
|
|
return counts
|
|
|
|
}
|
2024-01-23 08:00:09 -05:00
|
|
|
|
2023-06-27 09:37:53 -04:00
|
|
|
function buildURL(file, pdfDownloadDomain) {
|
2022-06-01 09:36:50 -04:00
|
|
|
if (file.build && pdfDownloadDomain) {
|
|
|
|
// Downloads from the compiles domain must include a build id.
|
|
|
|
// The build id is used implicitly for access control.
|
|
|
|
return `${pdfDownloadDomain}${file.url}`
|
|
|
|
}
|
|
|
|
// Go through web instead, which uses mongo for checking project access.
|
2023-01-17 10:23:51 -05:00
|
|
|
return `${window.origin}${file.url}`
|
2022-06-01 09:36:50 -04:00
|
|
|
}
|
|
|
|
|
2021-09-30 07:29:25 -04:00
|
|
|
function normalizeFilePath(path, rootDocDirname) {
|
2022-11-04 06:04:52 -04:00
|
|
|
path = path.replace(/\/\//g, '/')
|
2021-09-30 07:29:25 -04:00
|
|
|
path = path.replace(
|
|
|
|
/^.*\/compiles\/[0-9a-f]{24}(-[0-9a-f]{24})?\/(\.\/)?/,
|
|
|
|
''
|
|
|
|
)
|
|
|
|
|
|
|
|
path = path.replace(/^\/compile\//, '')
|
|
|
|
|
|
|
|
if (rootDocDirname) {
|
|
|
|
path = path.replace(/^\.\//, rootDocDirname + '/')
|
|
|
|
}
|
|
|
|
|
|
|
|
return path
|
|
|
|
}
|
2022-07-04 08:25:24 -04:00
|
|
|
|
|
|
|
function isTransientWarning(warning) {
|
|
|
|
return TRANSIENT_WARNING_REGEX.test(warning.message)
|
|
|
|
}
|